1👍
✅
Signals are a useful tool for this job (docs)
Using the post_save
signal you can run a function every time an instance of a model is saved, no matter where it is saved from. The post_save
signal provides the created argument so you only run the function when the object is created
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import DocumentDetailSample
@receiver(post_save, sender=DocumentDetailSample)
def my_handler(sender, **kwargs):
if kwargs['created']:
query_set = User.objects.all()
ids = query_set.values_list('id', flat=True)
for i in ids:
doc_detail = DocumentDetail()
doc_detail.details_sample = kwargs['instance']
doc_detail.user_id_id = str(i)
doc_detail.save()
Source:stackexchange.com