[Django] #31143: Variable attribute resolution priority in templates

2 views
Skip to first unread message

Django

unread,
Jan 8, 2020, 7:50:33 AM1/8/20
to django-...@googlegroups.com
#31143: Variable attribute resolution priority in templates
-------------------------------------+-------------------------------------
Reporter: Vasili | Owner: nobody
Korol |
Type: | Status: new
Uncategorized |
Component: Template | Version: 1.11
system | Keywords: template, variable
Severity: Normal | resolving
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Assume a Django model defining a `__getitem__` method:

{{{
class DataBlob ( models.Model ) :

# ... field & method definitions ...

def get ( self, key ) :
"""Returns a value by key from a msgpacked structure stored in a
file.
"""
# ....

def __getitem__ ( self, key ) :
"""Just a convenience shortcut for a method DataBlob.get.
"""
return self.get( key )
}}}

Now, we would like to access the `id` of a `DataBlob` instance in a
template:

{{{
<input type="hidden" id="VAL_datablob_id" value="{{datablob.id}}">
}}}

Expected result in the `value` property of the `<input>`: The id of the
object.
Observed: The result of calling `datablob['id']`.

The logic in `django.template.base.Variable._resolve_lookup` is to first
attempt a dictinary lookup, and then fallback to attribute lookup.

Why is it prioritized this way? In the current implementation, the
"dictionary-interpreted" keys will take precedence over object attributes,
masking them completely if the names are the same. I think it is more
logical to give priority to the attributes.

(I selected version 1.11 for this ticket, but i think it's the same for
2.2 and 3.0).

--
Ticket URL: <https://code.djangoproject.com/ticket/31143>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 8, 2020, 8:04:25 AM1/8/20
to django-...@googlegroups.com
#31143: Variable attribute resolution priority in templates.
-------------------------------------+-------------------------------------
Reporter: Vasili Korol | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: master
Severity: Normal | Resolution: wontfix
Keywords: template, variable | Triage Stage:
resolving | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 1.11 => master
* resolution: => wontfix


Comment:

This decision was made at the very beginning and it's
[https://docs.djangoproject.com/en/3.0/ref/templates/api/#variables-and-
lookups documented] since 2005 (see
096ad32c845f850278b942d087e607dc4311b5f9). If you need a different
behavior you can always create a custom template filter.

--
Ticket URL: <https://code.djangoproject.com/ticket/31143#comment:1>

Reply all
Reply to author
Forward
0 new messages