How to Add or Remove an Object From a ManyToManyField in Django



Python


In this article, we show how to add or remove an object from a ManyToManyField in Django.

A ManyToManyField is a very useful field in Django, because many real-life concepts work off of this concept. A ManyToManyField allows many objects to be in a given field. Unlike conventional fields such as firstname or lastname, only one value can be stored, because, after all, a person can only have 1 first name and 1 last name. However, other fields may hold a variety of objects. Think of a shopping cart where a vistor can place several different products in the shopping cart. Or think of a like button system for your website, where multiple users can like a post or a comment. This is the application of ManyToManyField.

In this article, we show how to add an item to a ManyToManyField and how to remove an item from a ManyToManyField.

We will be adding objects and removing objects from the following model in the models.py file shown below.



So in this models.py file, we have a model called ShoppingCart, which represents a shopping cart for an ecommerce site.

In this model, we have a row called products. This is a ManyToManyField, because a customer can buy several products on the site. We then pass into this ManyToManyField, the model Product, which represents a certain product.

A ManyToManyField functions just like a ForeignKey. It accepts another model item.


How to Add an Object to a ManyToManyField

Now let's see how to add an object to a ManyToManyField in Django.

To do so, Django has a built-in function, add(), which allows us to do so.

This would be done in the views.py file.

This is shown below.



So the first thing we have to do in the views.py file is import the ShoppingCart model. This is the model we're adding the products to.

We create a function-based view, addproduct, that adds a product to the shopping cart model.

This function takes the parameters, request and id. We pass in id because we need to have a way of identifying which product was selected. Because the id attribute is unique to the product, this is one way of identifying it. Another way would be with a slug, but we don't use that here.

So we get the specific product that the user added by the statement, productobj= get_object_or_404(Product, id=id)

The variable, productobj, now holds the specific product.

We create a variable, shoppingcart, and set it equal to, ShoppingCart(). This variable now references the ShoppingCart model.

We then add to the ShoppingCart model through the statement, shoppingcart.products.add(productobj)

We add to the products row of the ShoppingCart model. Therefore, we have, shoppingcart.products

The add() function allows us to add an object to this ManyToManyField.

And this is how to add an object to a ManyToManyField in Django.


How to Remove an Object From a ManyToManyField

Now we go over how to remove an object from a ManyToManyField in Django.

This works just as above but now the function is remove()

This is shown below.



So now we just change the function to removeproduct()

And we remove the product by the statement, shoppingcart.products.remove(productobj)

So this is how to add or remove objects from a ManyToManyField in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...