Use different hash for records without id
In order to have store them efficiently in dictionary, we generate a different hash for each unsaved record by using the instance id. issue9240 review295421002
This commit is contained in:
1 parent
23bac3a173
commit
4a1f2c938b
2 files changed
+25
-1
No files matched your search
@@ -316,7 +316,7 @@ class Model(URLMixin, PoolBase, metaclass=ModelMeta):
|
||||
return not self == other
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.__name__, self.id))
|
||||
return hash((self.__name__, id(self) if self.id is None else self.id))
|
||||
|
||||
def __bool__(self):
|
||||
return True
|
||||
|
||||
@@ -30,6 +30,30 @@ class ModelTestCase(unittest.TestCase):
|
||||
self.assertEqual(
|
||||
repr(record), "Pool().get('test.model')(%s)" % record.id)
|
||||
|
||||
@with_transaction()
|
||||
def test_hash_with_id(self):
|
||||
"Test hash with id"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model')
|
||||
|
||||
record1 = Model(id=1)
|
||||
record1bis = Model(id=1)
|
||||
record2 = Model(id=2)
|
||||
|
||||
self.assertEqual(hash(record1), hash(record1bis))
|
||||
self.assertNotEqual(hash(record1), hash(record2))
|
||||
|
||||
@with_transaction()
|
||||
def test_hash_without_id(self):
|
||||
"Test hash without id"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.model')
|
||||
|
||||
record1 = Model()
|
||||
record2 = Model()
|
||||
|
||||
self.assertNotEqual(hash(record1), hash(record2))
|
||||
|
||||
@with_transaction()
|
||||
def test_init_parent(self):
|
||||
"Test __init__ with _parent_"
|
||||
|
||||
Reference in new issue
Block a user