2👍
You could add a receiver to intercept the post save signal:
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Student)
def create_default_docs(sender, instance, created, **kwargs):
if created:
for title in ['title1','title2','title3']:
d = Document(title=title, student=instance)
d.save()
This code gets executed after the student model has been saved. If the model has just been created, it will then create the three documents with the default titles specified.
One thing to note, you’d need to allow your file field to allow nulls, as at the moment it’s a required field.
Source:stackexchange.com