#16389: In Django 1.3, dictionary lookup now calls the value if callable making
classes unusable
-------------------------------------+-------------------------------------
Reporter: iElectric | Owner: nobody
Type: Bug | Status: closed
Milestone: | Component: Template system
Version: 1.3 | Severity: Normal
Resolution: invalid | Keywords: template lookup
Triage Stage: | dictionary callable
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Changes (by lukeplant):
* status: new => closed
* needs_docs: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
That's correct. Yes, it was a significant change, but not a change in the
philosophy of the template at all - merely the conclusion of decisions we
had already made. #7153 has some background, and this discussion:
https://groups.google.com/d/topic/django-developers/rgkNIu4XBKI/discussion
There were various consistency problems. The main one was that passing
plain callables (e.g. a lambda or bound method) into a Context did not
work as expected, when the same bound method arrived at as part of lookup
done '''inside''' the template '''would''' get called. Another
inconsistency is the fact that if you get to a '''class''' via an
attribute lookup, it would get called (e.g. `{{ some_model_form.model }}`
would result in 'model' being called), but if that class was passed in as
a top level variable it would not get called. This was confusing and
unnecessary.
Using classes directly is a strange thing to do from the point of view of
a template author - what exactly is a 'class'? There is also an easy work-
around for your problem - create a simple dict/class with the attributes
of each model that you need e.g. instead of:
{{{
#!python
def all_models(request):
return dict([(klass.__name__, klass) for klass in get_models()])
}}}
do:
{{{
#!python
def all_models(request):
return dict([(klass.__name__, {'objects': klass.objects}) for
klass in get_models()])
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/16389#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.