[Django]-How to update a model instance in another model save method in django?

8👍

Overriding the save method on Voucher model and passing the VoucherTypeMaster and not its instance solved the problem:
Increment the last_number, if the self.id is None

def save(self, *args, **kwargs):
    try:
        voucher_type = VoucherTypeMaster.objects.get(
            company=self.company,
            code=self.type.code
            )
        if self.id is None:
            voucher_type.last_number = voucher_type.last_number+1
            self.type = voucher_type
            voucher_type.save()
    except Exception,e:
        print e
    super(Voucher, self).save(*args, **kwargs)

Leave a comment