django模板循环collections.defaultdict(lambda:collections.defaultdict(list))

时间:2013-08-23 15:05:17

标签: django django-templates django-views

在我看来,我从查询集

创建了以下列表字典
#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')

位置字段是以下格式的charFiled:X-X-X-X使用了创建my_dict

my_dict是

--[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

在我的模板中,我想做

{% 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 %}

结果是

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>]}) 

我尝试{% for cols in groups.1 %},但它无效(空)。 如果我做{%for cols in groups.iteritems%}我有:“Int不可迭代”。

查看https://code.djangoproject.com/ticket/16335我的案例是

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

我该如何解决?

提前致谢!

d

1 个答案:

答案 0 :(得分:0)

我的view.py解决方案是:

    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')

链接到https://code.djangoproject.com/ticket/16335Django template can't loop defaultdict

我的模板

{% 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 %}