From 382a6e206a66c5ecc609199d7bb0da19909f61f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 24 Sep 2025 09:09:05 +0200 Subject: [PATCH] Add total ordering on Model Closes #14254 --- proteus/CHANGELOG | 1 + proteus/proteus/__init__.py | 8 ++++++-- proteus/proteus/tests/test_model.py | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/proteus/CHANGELOG b/proteus/CHANGELOG index f400b9a119..cca4d93504 100644 --- a/proteus/CHANGELOG +++ b/proteus/CHANGELOG @@ -1,3 +1,4 @@ +* Add total ordering on Model Version 7.6.0 - 2025-04-28 -------------------------- diff --git a/proteus/proteus/__init__.py b/proteus/proteus/__init__.py index d428b51f27..b57a762ca7 100644 --- a/proteus/proteus/__init__.py +++ b/proteus/proteus/__init__.py @@ -7,6 +7,7 @@ import datetime import functools import threading from decimal import Decimal +from functools import total_ordering import proteus.config @@ -688,6 +689,7 @@ class ModelList(list): self._changed() +@total_ordering class Model(object): 'Model class for Tryton records' __slots__ = ('__id', '_values', '_changed', '_group', '__context') @@ -797,8 +799,10 @@ class Model(object): == (other.__class__.__name__, other.id)) return NotImplemented - def __ne__(self, other): - return not self == other + def __lt__(self, other): + if not isinstance(other, Model) or self.__class__ != other.__class__: + return NotImplemented + return self.id < other.id def __hash__(self): return hash(self.__class__.__name__) ^ hash(self.id) diff --git a/proteus/proteus/tests/test_model.py b/proteus/proteus/tests/test_model.py index 51d18ef671..51ed0bc4cd 100644 --- a/proteus/proteus/tests/test_model.py +++ b/proteus/proteus/tests/test_model.py @@ -286,6 +286,24 @@ class TestModel(ProteusTestCase): self.assertNotEqual(admin1, False) self.assertNotEqual(admin1, 1) + def test_lt(self): + User = Model.get('res.user') + + user1 = User(1) + user2 = User(2) + + self.assertLess(user1, user2) + + def test_lt_other_model(self): + User = Model.get('res.user') + Group = Model.get('res.group') + + user1 = User(1) + group2 = Group(2) + + with self.assertRaises(TypeError): + user1 < group2 + def test_default_set(self): User = Model.get('res.user') Group = Model.get('res.group')