# 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:** 8\
**Page:** 2

<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:36pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/21 "2022-01-25T15:36:03Z")

</div>

Sorry yes, this was a subject we worked on last week 😉

`Holding` has a fk to `Product`

```auto
class Holding(models.Model):

    product_name = models.ForeignKey(Product, on_delete=models.CASCADE)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    product_holding = models.IntegerField(blank=True, null=True)
    product_price = models.FloatField(max_length=50, blank=True, null=True)

    def __str__ (self):
        return str(self.product_name)

```

---

<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:39pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/22 "2022-01-25T15:39:36Z")

</div>

Ok, then for each **Product** , `holding_set` describes the Holdings for that product.

So as you’re iterating over each `item in products`, then `item.holding_set.all()` are the Holding associated with it.

---

<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:46pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/23 "2022-01-25T15:46:51Z")

</div>

> [@KenWhitesell](#):
>
> Ok, then for each **Product** , `holding_set` describes the Holdings for that product.

Yep thats it, so i want to show the holding information for that product.  
I want to display the price the user paid `product_price` and the `product_holding` along with current price from the `results` dictionary

My code is all over the place. cause i’ve been trying all different things and copied the wrong model information into the previous post. I’ve edited it.

---

<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:54pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/24 "2022-01-25T15:54:05Z")

</div>

You’ve got all the information you need from what’s been posted here. You should try at this point to put all the pieces together to see what you come up with.

---

<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, 5:14pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/25 "2022-01-25T17:14:22Z")

</div>

Sorry, Ken. I’m stuggling to work this out.

`holdings = profile.holding_set.all()` This holds all of the holdings related to the profile for each product

`Holding` has a FK to `Product` on `product_name` (which should be named product).

Therefore i need to access `product_holding` within `Holding` via `Product` using `item.something`?

like you said `item.holding_set.all()` but i dont understand what i need to pass in. `item.holding_set.product_holding` but this throws `object has no attribute product_holding`

---

<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, 5:59pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/26 "2022-01-25T17:59:11Z")

</div>

this doesn’t seem right, but it works.  
within in my `for loop` i am doing:

`holding = Holding.objects.get(product_name__product_name = item.product_name)`

and then within results `{...holdings:holding.product_holding}`

---

<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:** [February 6, 2022, 8:15pm UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/27 "2022-02-06T20:15:33Z")

</div>

Hi Ken,

I’ve decided I want to add some more data.

I have `Profile` `Product` , `Holding` models, and want to add a `Transaction` model that is linked to the `Profile` , and `Holding` model to be able to add purchased amount and price.

I’m not sure what the best way to link these models is to be able to easily present the data within a table.

**Models:**

```auto
class Product(models.Model):

    product_name = models.CharField(max_length=255, blank=False, unique=True)
    product_slug = models.CharField(max_length=50, blank=True,null=True)
    product_symbol = models.CharField(max_length=50, blank=True,null=True)
    product_price = models.FloatField(blank=True,null=True)
    product_capture_date = models.DateField(blank=True,null=True)
    product_logo_address = models.URLField(max_length=255, blank=True)

    def __str__ (self):
        return str(self.product_name)

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    products = models.ManyToManyField(Product, blank=True, through='Holding')
    ....

    def __str__ (self):
        return str(self.user)

class Holding(models.Model):

    product_name = models.ForeignKey(Product, on_delete=models.CASCADE)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
    product_holding = models.FloatField(max_length=50, blank=True, null=True)

    def __str__ (self):
        return str(self.product_name)

class Transaction(models.Model):

    product = models.ForeignKey(Holding, on_delete=models.CASCADE)
    amount = models.FloatField(max_length=50, blank=True, null=True)
    price = models.FloatField(max_length=50, blank=True, null=True)
    transaction_date = models.DateField(null=True, blank=True)
    profile = models.ForeignKey(Profile, on_delete=models.CASCADE)

```

I’ve created the transaction model and linked it with a FK to Holding and Profile - this seems to work for allocating the data to the model and allows me to add multiple transactions to a product for a profile.

But I think there is a better way to work within the view, bearing in mind that a product could have multiple transactions assigned and i wouldn’t all the transactions listed just the total amount and average price.

```auto
profile = Profile.objects.get(user = request.user)
transact = Transaction.objects.filter(profile = profile)

```

So within my table, I want to show the `product_name` , `transaction.price` , and `product_holding` . within the same view.

```auto
    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  

```

Im trying

```auto
     transactions = Transaction.objects.filter(product_name__product_name = item_product_name)

```

within the `for item in products`

---

<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:** [February 7, 2022, 9:55am UTC](https://forum.djangoproject.com/t/loop-through-queryset-and-store-data/11763/28 "2022-02-07T09:55:16Z")

</div>

Fixed this.

`transactions = Transaction.objects.filter(product__product_name_id = item.id)`

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