I recently had a need for a LazyObject which was callable.  The __call__
meta method isn't forwarded to _wrapped, so it's an error to call, even if
the underlying _wrapped does support it.

In my case, was trying to do the following:

User = SimpleLazyObject(lambda: get_user_model())

User()...

I patched LazyObject to make it happy:

def _a_call_(self, *args, **kwargs):
    if self._wrapped is empty:
        self._setup()
    return self._wrapped(*args, **kwargs)

LazyObject.__call__ = _a_call_.__get__(User)

Unfortunately, I then ran into a related problem -- by making LazyObject
callable, anyone passing a lazy user, e.g. request.user, to the ORM would
fall into the callable(value) branch:
https://github.com/django/django/blob/b77f26313cddbfde20dcf2661e9bd35458c2d1bd/django/db/models/sql/query.py#L1043

That then caused a new User to be constructed rather than using the
request.user.pk.

I see that callable criteria are deprecated there, but was wondering how
people would feel about adding a branch before that:

if ...
elif isinstance(value, LazyObject):
  pass
elif callable(value):
  ...

That would allow request.user to still be used (as it no doubt widely is)
even after LazyObject grows a callable (if its underlying object has one).

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAM0i3f7hshHzW9XsuRkJaT%2BaVEMetTKkzD7nUiDqMF9%2BzAR4rw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to