我是Django的新手,遇到了问题。 我在商品模型中有一个方法:
@transaction.atomic
def payment(self):
if self.owner.balance > self.price and self.active is False:
self.act_time_till = now() + timedelta(days=1)
self.owner.balance -= self.price
# here i need to create a new Invoice object
self.save()
return True
else:
return False
我有一个不同的模型 - 发票:
class Invoice(models.Model):
commodity = models.ForeignKey(Commodity, on_delete=models.PROTECT)
paid_till = models.DateTimeField(blank=False, null=False)
value = models.DecimalField(max_digits=3, decimal_places=2, blank=False, null=False)
每次付款时如何创建发票实例? 谢谢!
答案 0 :(得分:1)
# considering that you have invoice object available in the module. If not then you
# need to import it first.
Invoice.objects.create(commodity=self, paid_till=self.act_paid_till, value=self.price)
答案 1 :(得分:1)
你可以在这个方法中调用。
@transaction.atomic
def payment(self):
if self.owner.balance > self.price and self.active is False:
self.act_time_till = now() + timedelta(days=1)
self.owner.balance -= self.price
# here i need to create a new Invoice object
self.save()
## After this line you can create invoice object instance
Invoice.objects.create(commodity=self, paid_till=self.act_paid_till, value=self.price)
return True
else:
return False