#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.4b9e56c39266fb4224c9b1089a539746%40djangoproject.com.

Reply via email to