Model's ForeignKey field rises not-null constraint event with `default=` set
My model :
def Maker(models.Model):
name = models.CharField()
def get_default_maker():
default_maker, created = Maker.objects.get_or_create(name="noname")
return default_maker.pk
class Item(models.Model):
maker = models.ForeignKey(
Maker,
verbose_name="Maker",
on_delete=models.SET(get_default_maker),
default=get_default_maker
)
I have a Maker
instance-plug "noname". I would like to assign it on Item
creation if maker
field not set (in form). But whenever I try to create an Item without setting maker
field I get psycopg2.errors.NotNullViolation: null value in column "maker_id" of relation "stock_management_item" violates not-null constraint
.
Ofcourse, I can manage that in view/form but I would like to understand why default
value won't work.