#18864: Django models __eq__ and __hash__ treat all unsaved model instances as identical ----------------------------------------------+-------------------- Reporter: fengb | Owner: nobody Type: Bug | Status: new Component: Database layer (models, ORM) | Version: 1.4 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 ----------------------------------------------+-------------------- When a model instance is still new and unsaved, it has no primary key.
Both __eq__ and __hash__ rely solely on _get_pk_val(), which means all unsaved model instances are treated as though they are identical. >>> b1 = models.Base() >>> b2 = models.Base() >>> b1 == b2 True >>> hash(b1) == hash(b2) True For integrity, we should add a check for Python object identity since if both instances are saved, they will end up with different PKs. For hashing, we can use the default Python hashing to prevent large collisions based on hash(None). We would also need to cache the hash so that hash(instance) will be the same both before save and after save such that: >>> b = models.Base() >>> s = set() >>> s.add(b) >>> b.save() >>> b in set True This last case actually has an edge case that I'm not sure is solvable. >>> b = models.Base.object.get(id=b.id) >>> b in set False -- Ticket URL: <https://code.djangoproject.com/ticket/18864> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.