Reputation: 5468
I'm new to django and I'm having this error
IntegrityError: NOT NULL constraint failed: boxes_suggestion.box_id
this is my model
class Box(models.Model):
"""
Box model
"""
def __str__(self):
return self.title
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=40, blank=True, null=True)
slug = AutoSlugField(_('slug'), populate_from="id")
identify = models.BooleanField(default=False)
class Suggestion(models.Model):
"""
Suggestion model
"""
def __str__(self):
return self.content[0:10]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
box = models.ForeignKey(Box, on_delete=models.CASCADE)
content = models.CharField(max_length=250, blank=True, null=True)
A Box(Suggestion box) has many suggestions but when I try to create a Suggestion s = Suggestion(content=fake.text()).save()
I'm getting the error mentionned above
Upvotes: 1
Views: 132
Reputation: 13078
Suggestion.box
is a foreign key to Box
and cannot be null. When you save a Suggestion object, you must assign it to an already existing Box.
test_box = Box.objects.create(title='test box')
# now we can save suggestion
s = Suggestion(box=test_box, content=fake.text())
s.save()
Upvotes: 1