0

I am having following data:

   [{
    'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
    'user': [ < User: mpowner mpowner > ]
}, {
    'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
    'user': [ < User: kvermaOwner Owner > ]
}]

I want to iterate it in my django template. As shown these are two records with 2 keys in each(mp and user) each record belong to particular user. So I wnat to it such that I get mps of user and detail of user. But when I try to iterate it as explained in below answer or any other answer through out SO I every time get bizarre results. mp can contains further multiple records but I am stuck at first iteration only. I am very new to python , it is my 3rd day working. Any guidance would save my day.

As soon as I apply below:

{% for contributor in contributors.details %}

    {{ contributor }}

{% endfor %}

and I got this Output which break downs the structure:

  {
    'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
    'user': [ < User: mpowner mpowner > ]
} {
    'mp': [ < MpbMealPlan: MpbMealPlan object > , < MpbMealPlan: MpbMealPlan object > ],
    'user': [ < User: kvermaOwner Owner > ]
}

One query: Is it even possible to get desired results from the data I have. I want to iterate it to get mp and user and then I want to iterate mp to get multiple records within it. As whenever I tried iterating up to any level I get all the records so keys "mp and user" are not solving my purpose.

I don't want to waste any one's time here. I have updated the question. Thanks for answers till now. Update:

Following is the method I used and got desired results:

 {% for contributor in contributors.details %}

    {% for user in contributor.user %}
        {{ user }}
        <br>

        {% for mp in contributor.mp %}

            {{ mp.mp_name }}
            <br>
        {% endfor %}


    {% endfor %}

{% endfor %}

Results:

user:mpowner mpowner
mp:Fresh & Healthy
mp:evening snacks
user:kvermaOwner Owner
mp:Fresh & Healthy
mp:Energizing 

At last I got desired output with C14L's help (Amazing guidance). Thanks to "ajabdelaziz" and others too.

Always_a_learner
  • 4,585
  • 13
  • 63
  • 112

2 Answers2

1

For Python3, calling .items() gives you the items.

for key, item in data.items():

For Python2 use iteritems():

for k, v in knights.iteritems():

In Django template:

<div>
  {% for k,v in test.items %}
  <p>{{ k }} --> {{ v }}</p>
  {% endfor %}
</div>

Docs: https://docs.python.org/3/tutorial/datastructures.html#looping-techniques

Edit:

To add to the answer, looking at your specific object

[('details', [{
    'mp': u '[{"fields": {"status": 

look at this part: 'mp': u '[{"fields": {"st

That is not a list(), it is a string: u''

When you loop a string, you get the individual characters the string contains, one by one. Hence the "strange" result.

Edit 2:

Both contributor.mp and contributor.user contain lists

{% for contributor in contributors.details %}
    {{ contributor.mp }}
    {{ contributor.user }}
{% endfor %}

so, for example to print all usernames, you can do

{% for contributor in contributors.details %}
    {% for user in contributor.user %}
        {{ user.username }}
    {% endfor %}
{% endfor %}

To print the list of mp items, you can do

{% for contributor in contributors.details %}
    {% for mp in contributor.mp %}
        {{ mp }}
    {% endfor %}
{% endfor %}

But each mp object has probably a number of attributes. You need to look up what your MpbMealPlan class definition looks like.

C14L
  • 12,153
  • 4
  • 39
  • 52
  • I want to do this is django template. Will it work in template too? – Always_a_learner May 27 '16 at 08:36
  • Thanks again. I think the 'u' before 'mp' was because i was using serializers.serialize("json",model.getUserById(contributor_id)) to get data for 'mp'. I have updated my question. You are realy helping thanks! – Always_a_learner May 27 '16 at 15:22
  • You are almost there now. Just need to look up what structure you `MpbMealPlan` objects have. The `User` objects may be a standard Django `User`, so there is `username`, `first_name` etc on it. I'll add to my answer. – C14L May 27 '16 at 15:28
  • I know their structure I will get them once start getting single object at a time. I want to fetch mps and user out of one record first and then from second record seems like its not even possible with my structure, Thanks a ton btw, saved my many hours. – Always_a_learner May 27 '16 at 15:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113164/discussion-between-simer-and-c14l). – Always_a_learner May 27 '16 at 16:01
1

From what I'm seeing based on your error, is that you're trying to iterate over mp which has a value with dictionaries and lists in them. Because of the different data types, you cannot just use .items() only. You'll need to use .items() to get the mp's value in key,value. then you'll have to deal with iterating over the list then the key, values again. C14L's answer shows how to do that in templates just don't forget about your datatypes!

i.e -

   <div>
    {% for k,v in test.items %}
        {% for item in v%}
            {% item %}
        {% endfor %}    
    {% endfor %}
   </div>

item would be the list that you can then iterate over to get additional key value pairs.

Another possible solution is to do most of this in a template tag filter. and then just apply the filter onto the selected variable you want it to filter out.

ajabdelaziz
  • 134
  • 5