title: ValueError at /cart/add/3 "<Cart: None>" needs to have a value for field "id" before this many-to-many relationship can be used
body: <p>I am trying to make acart component for my ecommerce site
  this error appears:ValueError at /cart/delete/2
  "" needs to have a value for field "id" before this many-to-many relationship can be used.</p>
  
  <p>views.py</p>
  
  <pre><code>from django.shortcuts import render, redirect, reverse
  from django.urls import reverse_lazy
  from .models import Cart
  from products.models import Product
  from django.shortcuts import get_object_or_404
  def cart_home(request):
      cart_obj, new_obj=Cart.objects.new_or_get(request) 
      return render(request, "carts/home.html", {"cart":cart_obj})
  
  def addproduct(request, id):
      productobj= get_object_or_404(Product, id=id)
  
      shoppingcart=Cart()
  
      shoppingcart.products.add(productobj)
      shoppingcart.save()
      return redirect("carts:home")
  def removeproduct(request, id):
      productobj= get_object_or_404(Product, id=id)
      shoppingcart=Cart()
      shoppingcart.products.remove(productobj)
      shoppingcart.save()
      return redirect("carts:home")
  </code></pre>
  
  <p>''''
  models.py
  ''''</p>
  
  <pre><code>from django.db import models
  from django.conf import settings
  from products.models import Product
  from django.db.models.signals import pre_save, post_save, m2m_changed
  
  
  User = settings.AUTH_USER_MODEL
  class CartManager(models.Manager):
      def new_or_get(self, request):
          cart_id = request.session.get("cart_id", None)
          qs      = self.get_queryset().filter(id=cart_id)
          if qs.count() == 1:
              new_obj = False
              print('cart id exists')
              cart_obj = qs.first()
              if request.user.is_authenticated and cart_obj.user is None:
                  cart_obj.user = request.user
                  cart_obj.save()
          else:
              print("new cart created")
              new_obj = True
              cart_obj= Cart.objects.new(user=request.user)
              request.session['cart_id'] = cart_obj.id
          return cart_obj, new_obj
  
      def new(self, user=None):
          user_obj = None
          if user is not None:
              if user.is_authenticated:
                  user_obj = user_obj
          return self.model.objects.create(user=user_obj)
  class Cart(models.Model):
  
      user        = models.ForeignKey(User, null=True, blank=True, on_delete="Cascade")
      products    = models.ManyToManyField(Product, blank=True, null=True)
      total       = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
      updated     = models.DateTimeField(auto_now=True)
      timestamp   = models.DateTimeField(auto_now_add=True)
  
      objects = CartManager()
  
      def __str__(self):
          return str(self.id)
  def m2m_changed_cart_receiver(sender, instance, *args, **kwargs):
      products = instance.products.all()
      total=0
      for x in products:
          total += x.price
      print(total)
      instance.total = total
  m2m_changed.connect(m2m_changed_cart_receiver, sender=Cart.products.through)
  </code></pre>
  
  <p>''''
  carts:home.html
  ''''</p>
  
  <pre><code>{% extends "base.html" %}
  {% block content%}
  &lt;h1&gt;Cart&lt;/h1&gt;
  {% if cart.products.exists %}
  
              &lt;table class="table"&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th scope="col"&gt;#&lt;/th&gt;
                &lt;th scope="col"&gt;Product Name&lt;/th&gt;
                &lt;th scope="col"&gt;Product Price&lt;/th&gt;
  
              &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
              {% for product in cart.products.all %}
  
              &lt;tr&gt;
                &lt;th scope="row"&gt;{{forloop.counter}}&lt;/th&gt;
                &lt;td&gt;&lt;a href="{{product.get_absolute_url}}"&gt;{{product.title}}&lt;/a&gt;{% include "products/update_cart.html" %}&lt;/td&gt;
                &lt;td&gt;{{product.price}}&lt;/td&gt;
  
              &lt;/tr&gt;
              {% endfor %}
              &lt;tr&gt;
                &lt;th colspan="2"&gt;&lt;/th&gt;
  
                &lt;th&gt;&lt;b&gt;Total:&lt;/b&gt;{{cart.total}}&lt;/th&gt;
              &lt;/tr&gt;
  
            &lt;/tbody&gt;
          &lt;/table&gt;
  {% else %}
      &lt;div class="lead"&gt; Cart is empty&lt;/div&gt;
  {% endif %}
  
  
  {% endblock %}
  </code></pre>
  
  <p>''''
  cart-update.html</p>
  
  <p>''''</p>
  
  <pre><code>{% if product in cart.products.all %}
      &lt;form action="{% url 'carts:remove' product.id %}" 
                      method="post" style="display: inline;" onsubmit="window.mytest()"&gt;
                      {% csrf_token %}
                      &lt;input type="hidden" name="product_id" 
                          value="{{ product.id }}" /&gt;
                      &lt;button type="submit" class="btn btn-default btn-sm"&gt;
                          &lt;span class="fas fa-trash-alt"&gt;&lt;/span&gt;remove?
                      &lt;/button&gt;
              &lt;/form&gt; 
    {% else %}
              &lt;form action="{% url 'carts:add' product.id %}" 
                      method="post" style="display: inline;" onsubmit="window.mytest()"&gt;
                      {% csrf_token %}
                      &lt;input type="hidden" name="product_id" 
                          value="{{ product.id }}" /&gt;
                      &lt;button type="submit" class="btn btn-default btn-sm"&gt;
                          &lt;span class="fas fa-trash-alt"&gt;&lt;/span&gt;add to cart
                      &lt;/button&gt;
              &lt;/form&gt; 
  {% endif %}
  </code></pre>
  
  <p>''''
  so in product:detail template there is abutton enable me to add this product to cart or delete if it is there
  how to solve the above error also how to make button go to determined view? thanks in advance>>></p>