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.

4
  • 1
    Show us what you've tried so far Commented May 27, 2016 at 8:34
  • 1
    I think duplicate this question stackoverflow.com/questions/8018973/… Commented May 27, 2016 at 8:40
  • @se0kjun I have tried that answer but no help. Still get wrong results. If someone like I can paste answers I am getting after each for loop I applied. Commented May 27, 2016 at 14:09
  • 1
    show what code you have tried in your template, don't just say "it's not working", we don't know what you've tried to do Commented May 27, 2016 at 14:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to do this is django template. Will it work in template too?
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!
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.