Getting the original object from a proxy in Python

30 views
Skip to first unread message

Paul Walsh

unread,
Oct 31, 2012, 4:29:50 AM10/31/12
to pywe...@googlegroups.com
In a new project I am working with Django's proxy models, and it is just a great great way to build pragmatic interfaces for views and templates.

In a few cases, however, I need to do things like check equality with the original object, and my proxy is not actually equal to the original of course.

I can do this by checking equality on class properties, where that property is the same in the proxy and the original object. 

I wonder if there is a better practice in python. I found some suggestions to add a method on the original object that simply returns self, but my proxies, in this case, are on Django's User object.


Shai Berger

unread,
Oct 31, 2012, 7:19:29 AM10/31/12
to pywe...@googlegroups.com
On Wednesday 31 October 2012 10:29:50 Paul Walsh wrote:
> In a new project I am working with Django's proxy models, and it is just a
> great great way to build pragmatic interfaces for views and templates.
>
> In a few cases, however, I need to do things like check equality with the
> original object, and my proxy is not actually equal to the original of
> course.
>
> I can do this by checking equality on class properties, where that property
> is the same in the proxy and the original object.
>

With Django objects you can usually rely on the pk property (except if your
context involves unsaved objects).

> I wonder if there is a better practice in python. I found some suggestions
> to add a method on the original object that simply returns self

That, simply, won't work -- such a method will return the dynamic self (the
proxy), not an instance of the class in which it was defined (as might be the
case in some static languages).

HTH,
Shai.

Paul Walsh

unread,
Oct 31, 2012, 9:18:00 AM10/31/12
to pywe...@googlegroups.com
ok yes, I am using the pk now, so I guess this is the way to do it. thanks.
Reply all
Reply to author
Forward
0 new messages