I want to create a model within the save method of another model, so the one generated dynamically is named with a field from the static model.
Model Code:
class Car(models.Model):
name = models.CharField(max_length=128)
def save(self, *args, **kwargs):
attrs = {
'piece': models.CharField(max_length=128),
'__module__': 'myapp.models'
}
model = type('%s_piece' % self.name, (models.Model,), attrs)
admin.site.register(model)
super(Car, self).save(*args, **kwargs)
The model is generated but I don't know how to make the migrations or migrate it to the database.
I tried to migrate it with:
from django.core.management import call_command
call_command('makemigrations')
call_command('migrate')
But I get an error as I'm executing this in an atomic transaction.