How can I be able to use related_name
to create a child model when the parent model is being created.
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
class Parent(models.Model):
name = models.TextField()
child = models.ForeignKey(Child, on_delete=models.SET_NULL, related_name="parent_child", null=True, blank=True)
def save(self, *args, **kwargs):
super().save()
@receiver(post_save, sender=Parent)
def post_save_receiver(sender, instance, *args, **kwargs)
if not instance.child:
instance.child.parent_child.create(name="I am a child")
class Child(models.Model):
name = models.CharField()
Currently, on save, I get a NoneType
error:
'NoneType' object has no attribute 'parent_child'
These are the local variables in my stack trace
args: ()
instance: <Parent: asdf - asd-47013855>
kwargs:
{
'created': True,
'raw': False,
'signal': <django.db.models.signals.ModelSignal object at 0x7f20b92e2ce0>,
'update_fields': None,
'using': 'default'
}
sender: <class 'parent.models.Parent'>