[Answer]-How to save foreign key referenced model instance when parent model instance is created in Django

1👍

You need to create the Packet object (ie. calling the super() method) before creating the transaction. This way you can provide the right Packet object to the transaction you creates.

    if self.pk is None:
        from girvi.models import Transaction
        new_packet = super(Packet, self).save(*args, **kwargs)
        t = Transaction(type='0', description='0',
                        amount=self.total_worth, packet=new_packet,
                        created_on=self.created_on, updated_on=self.updated_on,
                        remark='First amount', roi_charged=self.roi_charged,
                        min_int_period=self.min_int_period)
        t.save()
    return 

Leave a comment