0

IntegrityError at /blablabla

NOT NULL constraint failed: product_product.category_id

Null=True, blank=True is suggested as a solution to this error.

But I don't want it to be null and blank. How do I solve it without adding blank and null? I previously defined the product list manually.

views.py:

Product.objects.create(code=product[1], 
   variant=product[2], header=str(product[3]) +" "+ str(product[2]),
   category_id=product[4], description=product[5],
  is_opportunity=product[6],is_outlet=product[7],discount=product[8],     price=product[12], image=str(product[1]) + "-" + str(product[2]) +".jpg")

models.py:

 class Product(models.Model):
    code = models.CharField(max_length=50,blank=True, null=True)
    variant = models.CharField(max_length=50)
    header = models.CharField(max_length=200)
    category = models.ForeignKey('Category', null=False, default=1, on_delete=models.CASCADE)
    image = models.ImageField(blank=True)
    description = models.TextField(null=True, blank=True)
    is_opportunity = models.BooleanField(null=True)
    is_outlet = models.BooleanField(null=True)
    slug = models.SlugField(unique=True, editable=False, max_length=150)
    discount = models.FloatField(null=True, blank=True)
    price = models.FloatField(null=True)
8
  • Providing a value that is different from None/NULL. Commented Jun 30, 2021 at 15:40
  • 1
    then you shouldn't left this field null Commented Jun 30, 2021 at 15:41
  • I don't leave it blank anyway but it gives this error :) I used objects.create() func. Commented Jun 30, 2021 at 15:42
  • Ok so show your code, could be another field causing this error but you are not helping us without any snippet of code Commented Jun 30, 2021 at 15:43
  • You are right. Sorry. Wait pls. Commented Jun 30, 2021 at 15:46

1 Answer 1

1

Two ways of doing it :

  1. Providing a value at save time, could be anything from a date to the username

  2. Set a default value to your field, for example :

    models.Charfield(default="Something")

Sign up to request clarification or add additional context in comments.

2 Comments

All my variables have default values
The error code is clear, you forgot to set a value for modal_modal.examples, so no, you don't have default values for everything, then again you are not showing any code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.