# Loop through queryset and store data

**URL:** <https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763>\
**Category:** Forms & APIs\
**Created:** [January 24, 2022, 1:03pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763 "2022-01-24T13:03:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 1:03pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/1 "2022-01-24T13:03:03Z")

</div>

Hi,

I have a list of products and for each product i make a `GET` request against a api to pull in product data. Such as current price and percent changed.

I then what to present the data in a table but i can’t find a way to loop through each product, make the request and store the data to be shown within a table.

I dont want to store the data in a model cause the data from the request will change daily, so it kinda needs to be realtime.

I’ve tried a few things, but can’t get it working.

`results = []`

```auto
    for item in products:        
        data = pt.get_product_by_id(item.product_slug)
        price = (data['product_data']['current_price']['GBP'])       
        day_change_percentage = (data['product_data']['change_percentage_24h'])
        results.append({item:price,item:day_change_percentage})

```

and then

```auto
{{% for item in results %}}
...

```

Is it even possible to do this?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 1:11pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/2 "2022-01-24T13:11:08Z")

</div>

Yes, it’s possible to do the following in a view:

- Retrieve data from the database
- Iterate over that data
- Make an api call using the data
- Augment the data
- Render the results

What exactly are you trying and what is going wrong?

Note: Regarding the code posted, the problem there appears to be some confusion with the syntax in what you’re trying to append to `results`. What are you trying to append there? A dict? If so, it’s not going to work the way you’re trying because you can’t use an object as the key and you can’t have multiple instances of the same key in a dict.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 1:25pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/3 "2022-01-24T13:25:13Z")

</div>

Hey Ken,

Basically, for each of my users they have a list of products. I am showing that list in a table, which you very kindly helped with (you’ve basically helped build my entire app. 🙂 )

As part of the list of items in the table, i am making a call out to an api that returns the current price and the change from yesterday.

So in my view i am getting the list of their products and then making the api call. I then need to take the response of each call for each product and show in the same table as the list of their products

```auto
Product Name | Current Price | % Changed |
product 1 API Reponse Api Response 
product 2 API Reponse Api Response  
product 3 API Reponse Api Response   

```

I was just trying to see if i can append the reponse of each request to the api to a variable so i can reference that variable in my template.

It doesn’t work.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 1:41pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/4 "2022-01-24T13:41:05Z")

</div>

> [@TommyQuality](#):
>
> I was just trying to see if i can append the reponse of each request to the api to a variable so i can reference that variable in my template.

Yes, you can do that.

You have at least two options for doing this:

- Add the data to the objects in the result set
- Create a new list of everything you want to render in the response.

Your post indicates you’re trying to do the second option.  
The problem with the snippet you posted is this line:

> [@TommyQuality](#):
>
> `results.append({item:price,item:day_change_percentage})`

for the reasons specified in my previous response.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 2:02pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/5 "2022-01-24T14:02:15Z")

</div>

> [@KenWhitesell](#):
>
> Create a new list of everything you want to render in the response.

Yep this is what i was trying to do. Is that the correct way (The way you would do it?)

So doing the append might be ok, but not using `Item` as the key? So it needs to be item.something?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 2:04pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/6 "2022-01-24T14:04:05Z")

</div>

You need to have a different key for each value.

Take a step back from Django for a moment and review what and how Python dictionaries work. Then think about how you want to represent your data within `results`. Then, and _only_ then, think about how you want to build that structure within your view.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 2:18pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/7 "2022-01-24T14:18:27Z")

</div>

I think i understand dictionaries, i know each key is unique and that data can be added/removed/changed after a dictionary has been created.

Within my view i added `results = dict{}` which i could then append the data to.

I think I’m also going to get into a mess cause i am trying to show both the list of products linked to that user, but that isn;t in this loop.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 2:22pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/8 "2022-01-24T14:22:48Z")

</div>

> [@TommyQuality](#):
>
> Within my view i added `results = dict{}` which i could then append the data to.

That’s actually not what you posted:

> [@TommyQuality](#):
>
> results =

`results` is a List, **not** a Dict. Each element within `results` is a dict, but `results` itself is not a dict. (You do not “append” to a dict. You add keys.)

> [@TommyQuality](#):
>
> I think I’m also going to get into a mess cause i am trying to show both the list of products linked to that user, but that isn;t in this loop.

Don’t worry about that yet.

Looking at:

> [@TommyQuality](#):
>
> ```auto
> Product Name | Current Price | % Changed |
> product 1 API Reponse Api Response 
> 
> ```

Substitute numeric values for those “API Response” strings, and show me what you think a dictionary would look like to represent just **this** one row.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 2:52pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/9 "2022-01-24T14:52:47Z")

</div>

> [@KenWhitesell](#):
>
> That’s actually not what you posted:

Sorry, i have been trying different approaches, getting mixed up.

I want the value in api response to be just the number, but i expect it will be `key:value`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 3:09pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/10 "2022-01-24T15:09:48Z")

</div>

I’m suggesting you ignore all other considerations at the moment.

Take the snippet I clipped previously of the one row of your representation, and replace that text “API Response” with numbers that make sense to you, and then show the Python code that would created your desired dictionary with that sample.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 10:35pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/11 "2022-01-24T22:35:50Z")

</div>

```auto
Product Name | Current Price | % Changed |
product 1 20 1.5

```

```auto
results = dict()
   for item in products:        
        data = pt.get_product_by_id(item.product_slug)
        price = (data['product_data']['current_price']['GBP'])       
        day_change_percentage = (data['product_data']['change_percentage_24h'])
        results.add({item.product_price:price,item.day_change_percentage:day_change_percentage})

```

```auto
{% for each ... %}
Product Name | Current Price | % Changed |
product 1 {{results.product_price}} {{results.day_changed_percentage}}

```

Im not really sure Ken.

But i see this wont work cause im not defining what the different keys are. So i need to understand how i create a new key/value for each item in products

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 10:50pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/12 "2022-01-24T22:50:18Z")

</div>

Forget the Django specific code at the moment.

You have this data:

> [@TommyQuality](#):
>
> ```auto
> Product Name | Current Price | % Changed |
> product 1 20 1.5
> 
> ```

What Python code could you write to create a dictionary of this data? Just this data - don’t rely upon anything other than what’s quoted here.

If necessary, review some documentation on Python dictionaries. Perhaps the tutorial section in the official Python docs at [5. Data Structures — Python 3.12.0 documentation](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 24, 2022, 11:20pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/13 "2022-01-24T23:20:43Z")

</div>

Im not sure, but i could just do `items = {'ProductName':'product 1', 'CurrentPrice:20','% Changed':1.5}`?

But that still have the issue of the keyName not being unique

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 24, 2022, 11:41pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/14 "2022-01-24T23:41:30Z")

</div>

Yes!

Starting from:

> [@TommyQuality](#):
>
> Im not sure, but i could just do `items = {'ProductName':'product 1', 'CurrentPrice':20,'% Changed':1.5}`

Now, if you keep `results` as a list, you can create this list, with each element being this dictionary:

```auto
results = [
   {'ProductName':'product 1', 'CurrentPrice':20, '% Changed':1.5},
   {'ProductName':'product 2', 'CurrentPrice':30, '% Changed':2.5},
   {'ProductName':'product 3', 'CurrentPrice':40, '% Changed':3.5},
]

```

Remember, the key names only need to be unique in a dictionary.  
In this case, you’re creating multiple dictionaries, all with the same keys to make it easier to refer to them in your template.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 25, 2022, 8:46am UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/15 "2022-01-25T08:46:39Z")

</div>

> [@KenWhitesell](#):
>
> Yes!

🙂

Right so i have created my list containing my dictionary using:

```auto
results = []
results.append = ({'productName':product ...})

```

That works so im hoping that is the correct way. My next hurdle is looping through whats in `Holding` and `results` to create the list of products in 1 table with the current price.

I assume i can do the same with `results` and append the output of `products` into the same list of dictionaries?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2022, 12:21pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/16 "2022-01-25T12:21:38Z")

</div>

> [@TommyQuality](#):
>
> `results.append = ({'productName':product ...})`

Close but not quite. `append` is a method on a list. It should be:  
`results.append({'productName':product ...})`

> [@TommyQuality](#):
>
> That works so im hoping that is the correct way. My next hurdle is looping through whats in `Holding` and `results` to create the list of products in 1 table with the current price.

You can build this dictionary with more fields from your model than just the name. You could also build the dictionary with a reference to the object itself.  
Example:  
If `product` is an instance of Product, then `{'product': product}` is a valid dictionary.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 25, 2022, 3:18pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/17 "2022-01-25T15:18:48Z")

</div>

> [@KenWhitesell](#):
>
> You can build this dictionary with more fields from your model than just the name. You could also build the dictionary with a reference to the object itself.  
> Example:  
> If `product` is an instance of Product, then `{'product': product}` is a valid dictionary

Im not quite sure on this.

for the `results` dictionary the key/values are appended within a `for` loop over products.

```auto
    profile = Profile.objects.get(user = request.user)
    products = profile.products.all()

```

```auto
for item in products:
...
results.append ({'productName':product ...})

```

but for `holdings` i am using `holdings = profile.holding_set.all()`

How do i add `holdings` to `results` so that i can use `{% for item in results %}` in my template to show `current price` and the price within `holding`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2022, 3:20pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/18 "2022-01-25T15:20:59Z")

</div>

Unfortunately, the snippets provided make it difficult to answer your question in context. Please post the complete view.

---

<div class="post-metadata">

**Author:** ![TommyQuality](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tommyquality/32/6302_2.png) [@TommyQuality](https://forum.djangoproject.com/u/TommyQuality)\
**Post date:** [January 25, 2022, 3:25pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/19 "2022-01-25T15:25:32Z")

</div>

Sorry Ken, here is the view

```auto
@login_required
def index(request):

    pt = productAPI()
    profile = Profile.objects.get(user = request.user)
    products = profile.products.all()
    holdings = profile.holding_set.all()
    results=[]
    for item in products:
        if item.product_slug is not None:
            data = pt.get_product_by_id(item.product_slug)
            price = (data['market_data']['current_price']['GBP'])
            day_change_percentage = (data['market_data']['price_change_percentage_24h'])
            results.append({'price':price,'day_change_percentage':day_change_percentage})
        else:
            pass    

    
    return render(request, 'index.html',{"holdings":holdings,"results":results})

```

Can i just add `"holdings.product_name":holdings_product_name` to the `results`? within the `for loop`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2022, 3:32pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/20 "2022-01-25T15:32:10Z")

</div>

I’m looking for the models for these and am not seeing them in a prior reply in this post.

Is there a relationship between products and holdings? If not, what you’re asking doesn’t seem to make sense to me. If there is, then you should be using _that_ relationship to retrieve the holdings instead of the global query.

[Next page](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763.md?page=2)
