Add total ordering on Model

Closes #14254
This commit is contained in:
Cédric Krier
2025-09-24 09:09:05 +02:00
parent 3d99b80027
commit 382a6e206a
3 changed files with 25 additions and 2 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add total ordering on Model
Version 7.6.0 - 2025-04-28
--------------------------
+6 -2
View File
@@ -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)
+18
View File
@@ -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')