# Items are not being added in the Cart.

**URL:** <https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564>\
**Category:** Using Django\
**Created:** [November 18, 2021, 10:09am UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564 "2021-11-18T10:09:23Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [November 18, 2021, 8:08pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/21 "2021-11-18T20:08:10Z")

</div>

OK, so should I store the cart object whose id is 8 from `carts` and then do `cart.two_piece_suit`?

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [November 18, 2021, 8:10pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/22 "2021-11-18T20:10:52Z")

</div>

@KenWhitesell well, I did and it returned this:

```auto
>>> mycart = ''
>>> for cart in carts:
... if cart.id == 8:
... mycart = cart
... 
>>> mycart.two_piece_suits
<django.db.models.fields.related_descriptors.create_forward_many_to_many_manager.<locals>.ManyRelatedManager object at 0x00000199FFC0ECD0>
>>>

```

What’s its use? it returned some line of code and not an actual `QuerySet` or a `List`.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [November 18, 2021, 8:24pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/23 "2021-11-18T20:24:56Z")

</div>

I also tried `mycart.two_piece_suits` but it returned and empty `QuerySet`:

```auto
>>> mycart = ''
>>> for cart in carts:
... if cart.id == 8:
... mycart = cart
... 
>>> mycart.two_piece_suit
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Cart' object has no attribute 'two_piece_suit'
>>> mycart.two_piece_suits
<django.db.models.fields.related_descriptors.create_forward_many_to_many_manager.<locals>.ManyRelatedManager object at 0x00000199FFC0ECD0>
>>> mycart.two_piece_suits.all()
<QuerySet []>
>>>

```

So it means that there are no `two_piece_suits` objects in the cart. But it shows them in the Django Administration. Is there something wrong @KenWhitesell?

---

<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:** [November 18, 2021, 8:26pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/24 "2021-11-18T20:26:45Z")

</div>

No, there isn’t anything wrong.

That you are seeing two options in the admin page does _not_ mean that they have been selected - only that they are options available to be selected.

As I wrote before:

> [@KenWhitesell](#):
>
> I can’t tell from that image whether you’ve selected those items in the list or not.

Apparently, from these other results, they have not been selected.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [November 18, 2021, 8:28pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/25 "2021-11-18T20:28:41Z")

</div>

So, how do I select `TwoPieceSuit()` instances so that they get attached with `Cart()`'s instance?

---

<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:** [November 18, 2021, 8:37pm UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/26 "2021-11-18T20:37:01Z")

</div>

The Admin site uses a standard “select multiple” box. The comment under the box on that page tells you how to select multiple items.

Also, for other options, there’s the admin docs at [The Django admin site | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#working-with-many-to-many-models)

To do it from within your code, review the appropriate docs at:

- [Model field reference | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/models/fields/#manytomanyfield)
- [Related objects reference | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/models/relations/#related-objects-reference)

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [November 19, 2021, 5:43am UTC](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564/27 "2021-11-19T05:43:22Z")

</div>

Hi @KenWhitesell thanks again for helping. From the documentation that you sent and after doing a little more research I found a way to add the `TwoPieceSuit` instances in the cart using `from django.db.models.signals`. I would like to share the code so that whoever read this thread, might find it useful.

This is not a good or direct way, I actually defined a `pre_save_cart_receiver()` method and then connected it to `m2m_changed` signal by passing `Cart` model as sender. Now whenever I click any `TwoPieceSuit` instance in the `Cart` table in Django Administration Interface and click ‘Save and Continue Editing’, it actually updates the `total` attribute of the cart.

Below is the code for `pre_save_cart_receiver()`:

```auto
from django.db.models.signals import m2m_changed

def pre_save_cart_receiver(sender, instance, action, *args, **kwargs):
    if action == "post_add" or action == "post_remove" or action == "post_clear":
        two_piece_suits = instance.two_piece_suits.all()
        total = 0
    
        for item in two_piece_suits:
            total = total + item.price

        instance.total = total
        instance.save()

m2m_changed.connect(pre_save_cart_receiver, sender=Cart.two_piece_suits.through)

```

[Previous page](https://forum.djangoproject.com/t/items-are-not-being-added-in-the-cart/10564.md?page=1)
