title: django NOT NULL constraint failed:
body: <p>I am getting 2 errors trying to add products to my cart in a website in django.</p>
  <p>IntegrityError at /apiadd/
  NOT NULL constraint failed: cianshop_basketitem.basket_id_id</p>
  <p>in my console i see this:
  &quot;
  productindividual?id=1:136          POST <a href="http://127.0.0.1:8000/apiadd/" rel="nofollow noreferrer">http://127.0.0.1:8000/apiadd/</a> 500 (Internal Server Error)
  addToCart.onclick @ productindividual?id=1:136
  VM55:1 Uncaught (in promise) SyntaxError: Unexpected token I in JSON at position 0
  &quot;</p>
  <p>this is my models.py file:</p>
  <pre><code>from dataclasses import is_dataclass
  from django.db import models
  from django.contrib.auth.models import AbstractUser
  
  class APIUser(AbstractUser):
      pass
  
  class Product(models.Model):
      id = models.IntegerField(primary_key=True)
      name = models.TextField()
      description = models.TextField(null=True)
      price = models.DecimalField(max_digits=6, decimal_places=2, default=0.0) #9999.99
      image = models.FileField(upload_to='products', null=True)
  
  class Basket(models.Model):
      id = models.IntegerField(primary_key=True)
      user_id = models.ForeignKey(APIUser, on_delete=models.CASCADE)
      is_active = models.BooleanField(default=True)
  
  class BasketItem(models.Model):
      id = models.IntegerField(primary_key=True)
      basket_id = models.ForeignKey(Basket, on_delete=models.CASCADE)
      product_id = models.ForeignKey(Product, on_delete=models.CASCADE)
      quantity = models.IntegerField(default=1)
  
      def price(self):
          return self.product_id.price * self.quantity
      
      def product_name(self):
          return self.product_id.name
  
  class Order(models.Model):
      id = models.IntegerField(primary_key=True)
      basket_id = models.ForeignKey(Basket, on_delete=models.CASCADE)
      user_id = models.ForeignKey(APIUser, on_delete=models.CASCADE)
      datetime_ordered = models.DateTimeField(auto_now_add=True)
      total_price = models.DecimalField(max_digits=6, decimal_places=2, default=0.0)
      shipping_addr = models.TextField(default=&quot;&quot;)
  </code></pre>
  <p>this is my basket.ejs file:</p>
  <pre><code>  &lt;script&gt;
      window.onload = ()=&gt;{
        let params = window.location.search;
        let urlParams = new URLSearchParams(params);
        let productID = urlParams.get(&quot;id&quot;);
        // http://127.0.0.1:8000/api/products/id 
        if(productID != null &amp;&amp; typeof(productID)!= 'undefined'){
            fetch('http://127.0.0.1:8000/api/products/'+productID)
            .then(resp =&gt; resp.json())
            .then(data =&gt; {
                // console.log(data);
                if('detail' in data){
                    // display some generic product not found error
                    alert(&quot;Product Not Found!&quot;);
                }
                else{
                    let name = data['name'];
                    let desc = data['description'];
                    let price = data['price'];
                    // let image= data['image'];
                    // display the product data 
                    let nameElem = document.getElementById(&quot;myname&quot;); // returns the specific DOM node , the P tag with id=&quot;myparagraphid&quot;
                    nameElem.innerHTML=name;
                    let descElem = document.getElementById(&quot;mydesc&quot;); // returns the specific DOM node , the P tag with id=&quot;myparagraphid&quot;
                    descElem.innerHTML=desc;
                    let priceElem = document.getElementById(&quot;myprice&quot;); // returns the specific DOM node , the P tag with id=&quot;myparagraphid&quot;
                    priceElem.innerHTML=price;
                    document.getElementById(&quot;myimg&quot;).src = data[&quot;image&quot;];
                }
              });
        }
  
        let addToCart = document.createElement(&quot;button&quot;); // create a button
        document.getElementById(&quot;mybutton&quot;).appendChild(addToCart)
        addToCart.innerHTML = &quot;Add to cart&quot;; // set the text of the button 
        addToCart.onclick = ()=&gt;{
          // code for when the button is clicked
          fetch('http://127.0.0.1:8000/apiadd/', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'Accept': 'application/json',
              'Authorization': 'Bearer '+localStorage.getItem(&quot;access&quot;)
            },
            body: JSON.stringify({
              &quot;product_id&quot;: parseInt(productID)
            })
          })
          .then(response=&gt;response.json())
          .then(data=&gt;{
              // display an add to cart success message
              // console.log(response)
            });
        }
      }
  
    &lt;/script&gt;
  </code></pre>