django template loop collections.defaultdict(lambda: collections.defaultdict(list))

In my view I create the following dictionary of lists from a queryset

#view.py
queryset = MyModel.objects.filter(owner=user, dashboard=tab).order_by('position')
my_dict = collections.defaultdict(lambda: collections.defaultdict(list))    
for obj in queryset:
    my_dict[int(obj.position.split('-')[0])][int(obj.position.split('-')[2])].append(obj)
return Response({'queryset': dict(my_dict)}, template_name='dashboard/_maps_tab.html')

Position field is a charFiled following the format: X-X-X-X used the create the my_dict

my_dict is

--[1] #group
----[1] #col 1
------ object1.1.1 #group.col.pk
------ object1.1.2
------ object1.1.3
----[2] #col 2
------ object1.2.4
----[3] #col3
------ object1.3.5
------ object1.3.6

--[2] #group
----[1] #col 1
------object2.1.7 #group.col.pk

--[3] #group
----[1] #col1
------ object3.1.8 #group.col.pk
----[2] #col2
------object3.2.9
------object3.2.10

In my template I would like to do

{% for groups in queryset.iteritems %}

    groups = {{ groups }} <br>

    {% for cols in groups %}

      cols = {{ cols }} <br>

      {% for objs in cols %}

        {{ objs }} in <br><br>

        {% for obj in objs %}        
          {{ obj.title }}, 
          {{ obj.desc}}, 
          {{ obj.fieldN }},         

        {% endfor %}

      {% endfor %}


    {% endfor %}

{% endfor %}

The result is

groups = (1, defaultdict(<type 'list'>, {1: [<Obj: Obj 1 by daviddd>, <Obj: Obj 2 by daviddd>, <Obj: Obj3 by daviddd>], 2: [<Obj: Obj 4 by daviddd>], 3: [<Obj: Obj 5 by daviddd>, <Obj: Obj 6 by daviddd>, <Obj: Obj 7 by daviddd>]}))
cols = 1
cols = defaultdict(<type 'list'>, {1: [<Obj: Obj 1 by daviddd>, <Obj: Obj 2 by daviddd>, <Obj: Obj3 by daviddd>], 2: [<Obj: Obj 4 by daviddd>], 3: [<Obj: Obj 5 by daviddd>, <Obj: Obj 6 by daviddd>, <Obj: Obj 7 by daviddd>]})

groups = (2, defaultdict(<type 'list'>, {1: [<Obj: Obj 7.7 by daviddd>]}))
cols = 2
cols = defaultdict(<type 'list'>, {1: [<Obj: Obj 7.7 by daviddd>]})

groups = (3, defaultdict(<type 'list'>, {1: [<Obj: Obj 7.8 by daviddd>]}))
cols = 3
cols = defaultdict(<type 'list'>, {1: [<Obj: Obj 7.8 by daviddd>]}) 

I tried to do {% for cols in groups.1 %} but it's not working (empty). If I do {% for cols in groups.iteritems %} I have: "Int is not iterable".

Looking at https://code.djangoproject.com/ticket/16335 my case is

my_dict = collections.defaultdict(lambda: collections.defaultdict(list))    
dictionary['foo']['foo1'].append('bar')

How can I solve?

Thanks in advance!

D

Answers


My view.py solution was:

    my_dict = collections.defaultdict(lambda: collections.defaultdict(list))

    for obj in queryset:
        my_dict[int(obj.position.split('-')[0])][int(obj.position.split('-')[2])].append(obj)

    for obj in my_dict:            
        my_dict[obj].default_factory = None

    return Response({'queryset': dict(my_dict)}, template_name='_internal_template.html')

Linked to https://code.djangoproject.com/ticket/16335 and Django template can't loop defaultdict

My template

{% for groups in queryset.itervalues  %}

    groups = {{ groups }}    
    <br><br>

    {% for cols in groups.itervalues  %}

      cols = {{ cols }}    
      <br><br>

      {% for obj in cols  %}

        obj = {{ obj}} in <br><br>
        obj info = {{ obj.title }}, {{ obj.abstract }}<br>

      {% endfor %}      

    {% endfor %}

{% endfor %}

Need Your Help

Getting dock area where an MFC CDockablePane was docked

c++ user-interface mfc dock

I am working on a project that totally does not want to use the built-in window/dock state and position saving of MFC. This means that in my main frame, I set EnableLoadDockState(FALSE). As much as I

Visual Studio JavaScript Intelliense for VB generated controls

javascript asp.net-mvc asp.net-mvc-4 visual-studio-2012 intellisense

We have a user control system in place that allows us to create user controls which generate the HTML and JS necessary for the user control to function. The initial values or options are set in VB/...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.