mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed model.__eq__ and __hash__ for no pk value cases
The __eq__ method now considers two instances without primary key value equal only when they have same id(). The __hash__ method raises TypeError for no primary key case. Fixed #18864, fixed #18250 Thanks to Tim Graham for docs review.
This commit is contained in:
@@ -708,9 +708,20 @@ class ModelTest(TestCase):
|
||||
SelfRef.objects.get(selfref=sr)
|
||||
|
||||
def test_eq(self):
|
||||
self.assertEqual(Article(id=1), Article(id=1))
|
||||
self.assertNotEqual(Article(id=1), object())
|
||||
self.assertNotEqual(object(), Article(id=1))
|
||||
a = Article()
|
||||
self.assertEqual(a, a)
|
||||
self.assertNotEqual(Article(), a)
|
||||
|
||||
def test_hash(self):
|
||||
# Value based on PK
|
||||
self.assertEqual(hash(Article(id=1)), hash(1))
|
||||
with self.assertRaises(TypeError):
|
||||
# No PK value -> unhashable (because save() would then change
|
||||
# hash)
|
||||
hash(Article())
|
||||
|
||||
class ConcurrentSaveTests(TransactionTestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user