details:   https://code.tryton.org/tryton/commit/6e259d80adc1
branch:    default
user:      Cédric Krier <[email protected]>
date:      Wed Sep 24 09:09:05 2025 +0200
description:
        Add total ordering on Model

        Closes #14254
diffstat:

 proteus/CHANGELOG                   |   1 +
 proteus/proteus/__init__.py         |   8 ++++++--
 proteus/proteus/tests/test_model.py |  18 ++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)

diffs (68 lines):

diff -r 51805c42f099 -r 6e259d80adc1 proteus/CHANGELOG
--- a/proteus/CHANGELOG Mon Oct 13 09:41:41 2025 +0200
+++ b/proteus/CHANGELOG Wed Sep 24 09:09:05 2025 +0200
@@ -1,3 +1,4 @@
+* Add total ordering on Model
 
 Version 7.6.0 - 2025-04-28
 --------------------------
diff -r 51805c42f099 -r 6e259d80adc1 proteus/proteus/__init__.py
--- a/proteus/proteus/__init__.py       Mon Oct 13 09:41:41 2025 +0200
+++ b/proteus/proteus/__init__.py       Wed Sep 24 09:09:05 2025 +0200
@@ -7,6 +7,7 @@
 import functools
 import threading
 from decimal import Decimal
+from functools import total_ordering
 
 import proteus.config
 
@@ -688,6 +689,7 @@
             self._changed()
 
 
+@total_ordering
 class Model(object):
     'Model class for Tryton records'
     __slots__ = ('__id', '_values', '_changed', '_group', '__context')
@@ -797,8 +799,10 @@
                 == (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 -r 51805c42f099 -r 6e259d80adc1 proteus/proteus/tests/test_model.py
--- a/proteus/proteus/tests/test_model.py       Mon Oct 13 09:41:41 2025 +0200
+++ b/proteus/proteus/tests/test_model.py       Wed Sep 24 09:09:05 2025 +0200
@@ -286,6 +286,24 @@
         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')

Reply via email to