Items are not being added in the Cart.

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

@KenWhitesell well, I did and it returned this:

>>> 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.

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

>>> 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?

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:

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

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

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

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

1 Like

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():

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)