Opened 10 years ago
Last modified 10 years ago
#23592 closed Bug
Django 1.5 Proxy equality and Caching of related instances — at Version 1
Reported by: | Cisco Guillaume | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.5 |
Severity: | Normal | Keywords: | proxy equality |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Hello there :)
This is the first time I write a ticket regarding Django code, so I'll try to be as concise as possible.
When upgrading to django 1.5 for our company project, we ran in an unexpected behaviour.
We use a proxy model and for our needs, we use equality operator in the code.
Let's say I have a Person Model and a OrderedPerson Model which is a proxy of the Person Model.
a = Person() b = OrderedPerson()
We have two equal objects (id and dict are similar) so b == a should be equal to True (correct in Django 1.4).
However we get:
b == a <- False
Due to this commit posted in Django 1.5 version: https://github.com/django/django/commit/056ace0f395a58eeac03da9f9ee7e3872e1e407b
File django/db/models/query.py Lines 327-336
In reality, it is far more complex, because we have a test like:
def __setattr__(self, name, value): toto = getattr(self, name, None) == value # some code with toto variable
I found this commit solve the problem regarding proxy equality in Django 1.7:
https://github.com/django/django/commit/4668c142dce77c6f29fb75532c1acfa1b2d322ff#diff-507b415116b409afa4f723e41a759a9eR338
File django/db/models/base.py Lines 462-464
So we use it for our proxy models in Django 1.5
I think it could be very useful to add this piece of code, for versions from Django 1.5.
Cheers.
PS: I used these sources for understanding the origin of this "bug":
http://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes
http://stackoverflow.com/questions/3588776/how-is-eq-handled-in-python-and-in-what-order/12984987#12984987
https://docs.djangoproject.com/en/1.7/topics/db/models/#proxy-models