I have two models in different apps: ModelA
and ModelB
. They have a one-to-one relationship. Is there a way django can automatically create and save ModelB
when ModelA
is saved?
class ModelA(models.Model):
name = models.CharField(max_length=30)
class ModelB(models.Model):
thing = models.OneToOneField(ModelA, primary_key=True)
num_widgets = IntegerField(default=0)
When I save a new ModelA
I want a entry for it to be saved automatically in ModelB
. How can I do this? Is there a way to specify that in ModelA
? Or is this not possible, and I would just need to create and save ModelB
in the view?
Edited to say the models are in different apps.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…