Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Model proxy instance does not equal the respective model instance

40 views
Skip to first unread message

Byron

unread,
Oct 17, 2010, 12:00:12 PM10/17/10
to Django developers
I came across an issue when comparing a model proxy instance and a
model instance, that is:

obj1 = Entry.objects.get(id=1)
obj2 = EntryProxy.objects.get(id=1)

obj1 == obj2 # False

This I feel is a bug and the culprit is in ``Model.__eq__`` where it
tests to see if the object being compared against is an instance of
the same class of the Model itself.. which will always be false for
proxy models being compared to there non-proxied instance
counterparts. It should test if it is a proxy and whether the object
is an instance of the parent class as well.

Was this a design decision or overlooked?

Byron

unread,
Oct 19, 2010, 9:08:50 AM10/19/10
to Django developers
Added ticket and patch: http://code.djangoproject.com/ticket/14492

Byron

unread,
Oct 20, 2010, 9:24:23 AM10/20/10
to Django developers
Continuing from... http://code.djangoproject.com/ticket/14492

> the real issue here is "What is a Proxy", and "what is equality"

I agree, that is the real issue. I based most of my reasoning off of
what the docs already state in that a proxy model can be written to
extend the behavior of the model being proxied, i.e. the data model
cannot be changed. That being said, I feel the default behavior when
dealing with equality, other data related things... should inherit
from there parent. If there is a need to override this behavior, I am
sure other hooks can be implemented to customize proxy models
differently then there parent. With regards to my primary issue of
equality, in this context two objects are being compared to determine
whether they represent the same row of data. It seems less likely to
compare two model objects to only be interested in whether they are
equal data structures.

Regarding permissions, the arguments presented in http://code.djangoproject.com/ticket/11154
all can be accomplished with writing a custom manager for the proxy
model or writing a custom ModelAdmin class for the admin. I agree with
Malcolm that a proxy model's permission should shadow its parent.

Gabriel Hurley

unread,
Oct 20, 2010, 4:21:56 PM10/20/10
to Django developers
Speaking on a semantic level, a "proxy" is a stand-in acting on behalf
of (or in place of) another entity. There is an implied near-
equivalence, but inherent in the idea of a proxy is that it is *not*
the same as the original. As in the case of assigning a proxy to vote
for you in corporate elections: the proxy you delegate your right to
vote to is the functional equivalent of you, but they are recognized
as not *actually* being you.

In my mind the same reasoning applies to Proxy Models. While they are
a stand-in for accessing the same underlying data, they may have very
different properties (ordering, methods, etc.). So while they may pass
a rough equivalence test, they are in specific ways dissimilar. The
underlying table/data stored by the model is only one piece of what
makes up the sum total of a model.

That said, I can certainly see the use case for comparing proxy models
as equal to the original model...

All the best,

- Gabriel

On Oct 20, 6:24 am, Byron <bjr...@gmail.com> wrote:
> Continuing from...http://code.djangoproject.com/ticket/14492
>
> > the real issue here is "What is a Proxy", and "what is equality"
>
> I agree, that is the real issue. I based most of my reasoning off of
> what the docs already state in that a proxy model can be written to
> extend the behavior of the model being proxied, i.e. the data model
> cannot be changed. That being said, I feel the default behavior when
> dealing with equality, other data related things... should inherit
> from there parent. If there is a need to override this behavior, I am
> sure other hooks can be implemented to customize proxy models
> differently then there parent. With regards to my primary issue of
> equality, in this context two objects are being compared to determine
> whether they represent the same row of data. It seems less likely to
> compare two model objects to only be interested in whether they are
> equal data structures.
>
> Regarding permissions, the arguments presented inhttp://code.djangoproject.com/ticket/11154

Tai Lee

unread,
Oct 21, 2010, 12:09:08 AM10/21/10
to Django developers
I think that since Proxy models only affect the methods, properties
and way the data is used (not the actual underlying data itself), it
is appropriate to compare User and UserProxy as equal. To not treat
them as equal, you will need to be sure that you never pass a
UserProxy object to a 3rd party function that will attempt to compare
it to a User object. If you need to in your own code, you can always
use isinstance in combination with equality to determine how equal the
two objects are.

Cheers.
Tai.
Reply all
Reply to author
Forward
0 new messages