I am writing a Django app that provides a very simple model called Submittable
, which users of the app are supposed to inherit from when they want to use other functionalities of the app.
# app1
class Submittable(models.Model):
is_submitted = models.BooleanField(default=False)
# other methods here
# app2
class Paper(Submittable):
# some fields
However, when I add this as a parent to an already existing model in another app and run makemigrations
, I am asked to provide a default value to the new field submittable_ptr_id
. The problem is, that I want this field to simply point to a new instance of Submittable
but I don't know how to do that.
I know, that I could simply edit the migration file created like so:
class Migration(migrations.Migration):
dependencies = [
# some dependencies
]
operations = [
migrations.AddField(
model_name='Paper',
name='submittable_ptr_id',
# here I set default to a function from the Submittable app that just creates a new submittable
field=models.OneToOneField(auto_created=True, default=app1.utils.create_submittable, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key
=True, serialize=False, to='app1.Submittable'),
preserve_default=False
),
]
But I want to know if I can specify anything somewhere in app1
that makes this happen automatically? I don't want users of the app to have to make this change themselves and instead, whenever someone inherits from Submittable
and runs makemigrations
the default value should just be set to this callable that creates a new Submittable
.