Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a django.utils.functional.__proxy__ object and what it helps with?

Tags:

python

django

I stumbled across a django.utils.functional.__proxy__ object many times, last time in the following bit of code:

def formfield_for_choice_field(self, db_field, request, **kwargs):
    print db_field.help_text

(With the result of the print being <django.utils.functional.__proxy__ object at 0x7fc6940106d0>)

Now, why are things wrapped around this object? What is the main purpose of it? And more important, how can I access the value behind the proxy object? I viewed Django's source, I've seen something about lazy evaluation, but there are too many wrappers and wrapped objects to fully comprehend what's going on. Any details on this would be highly appreciated.

like image 317
linkyndy Avatar asked Mar 19 '14 09:03

linkyndy


People also ask

What is Django utils?

Django Utils is a collection of small Django helper functions and classes which make common patterns shorter and easier.

What is SimpleLazyObject?

SimpleLazyObject , itself is a subclass of LazyObject . LazyObject is, as described by the actual code: A wrapper for another class that can be used to delay instantiation of the wrapped class.


1 Answers

It is a translation string – a string that has been marked as translated but whose actual translation result isn’t determined until the object is used in a string. See Django documentation: https://docs.djangoproject.com/en/dev/ref/unicode/#translated-strings.

Calling unicode(object) will generate a Unicode string. Also, if you call __dict__ of the object, you can find its data under _proxy____args.

like image 103
iceui2 Avatar answered Sep 18 '22 19:09

iceui2