Hello, I am new to django so this might be a very basic question, but I found the models.SET_DEFAULT Value for the on_delete Parameter for ForeignKeys. Unfortunatly I don't understand how to use it properly.
I thought it allowed me to create an object (and save it to the database) and then set this object as the default Parameter.
models.py:
from django.db import models
class Bar(models.Model):
pass
class Foo(models.Model):
bar = Bar()
bar.save()
bar = models.ForeignKey(Bar, on_delete=models.SET_DEFAULT, default=bar)
but it throws the following error:
ValueError: Cannot serialize: <Bar: Bar object (3)>
There are some values Django cannot serialize into migration files
I didn't find any resources reguarding that topic online, but the github projects I found all used the id of the ForeignKey Object like so:
from django.db import models
class Bar(models.Model):
pass
class Foo(models.Model):
bar_id = models.ForeignKey(Bar, on_delete=models.SET_DEFAULT, default=1, null=True)
my question is: is this the only way possible to set a default value for a ForeignKey? And my second question: Is there a way so the migrations create that first default object automaticly when you first populate the dataabes?