0

I am using an order default dict from here . The problem is that i don't know how to access the objects.

I am expecting that something like this should work

{% for zone in data %}
    {% for reservation in zone %}
        {{reservation}} # 1 | 2| 3
    {% endfor %}
{% endfor %}

Data to help the debug

{{data}}

OrderedDefaultDict(<type 'list'>, DefaultOrderedDict([('1', [<app.backoffice.models.Reservation object at 0x7f91c2c5ee10>, <app.backoffice.models.Reservation object at 0x7f91c2c732d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73510>]), ('2', [<app.backoffice.models.Reservation object at 0x7f91c2c73790>, <app.backoffice.models.Reservation object at 0x7f91c32f9c50>]), ('3', [<app.backoffice.models.Reservation object at 0x7f91c2c733d0>, <app.backoffice.models.Reservation object at 0x7f91c2c73490>])]))

{% for zone in data %}
   {{zone}} # 1 | 2 | 3
   {{zone[0]}} # 1 | 2 | 3
{% endfor %}
Community
  • 1
  • 1
user2990084
  • 2,699
  • 9
  • 31
  • 46

1 Answers1

6

When you loop over a dictionary (even a subclass) you get keys; you'd have to translate that key into a value first if you want to loop over nested objects:

{% for zone in data %}
    {% for reservation in data[zone] %}
        {{reservation}}
    {% endfor %}
{% endfor %}

Since you don't display the zone key, you may as well loop over the dictionary values (using dict.itervalues() to avoid creating a redundant list object):

{% for reservations in data.itervalues() %}
    {% for reservation in reservations %}
        {{reservation}}
    {% endfor %}
{% endfor %}

or use dict.iteritems() to get both the key and the value:

{% for zone, reservations in data.iteritems() %}
    {{zone}}: 
    {% for reservation in reservations %}
        {{reservation}}
    {% endfor %}
{% endfor %}

In your own attempts, zone was only ever set to each key, which in your case are single-character strings ('1', '2' and '3'). Looping over a single-character string or indexing that string with zone[0] will only result in that one character to be shown.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343