Monday, August 26, 2013

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



On Friday, August 23, 2013 5:58:38 PM UTC+2, Daviddd wrote:
Dear All,

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 tried:

{% for key, groups in queryset.iteritems %}      groups = {{ groups }} <br>      {% for group_key, cols in groups.iteritems %}        cols = {{ group_key }} <br>        {% for objs in cols %}            {# rest of the code #}  

But only the first loop is evaluated

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>]}))    groups = (2, defaultdict(<type 'list'>, {1: [<Obj: Obj 7.7 by daviddd>]}))    groups = (3, defaultdict(<type 'list'>, {1: [<Obj: Obj 7.8 by daviddd>]}))  


I'm not able to spot the error.

Thanks

D




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment