【发布时间】:2016-04-28 04:52:18
【问题描述】:
我最近一直在玩 Django,但是我一直在思考如何解决这个问题。我有一个“人”模型,它与“凭证”模型具有一对多的关系。现在这个人有一个配额,每次生成一个 Voucher,配额就会减少一个。我坚持的是通过 save 方法做到这一点。那么如何做到这一点呢?
以下是我的模型:
class Person(AbstractDetail):
# Note: Model to maintain information about Person
vc = models.IntegerField(default = 3, verbose_name = 'Vouchers',
null = False, validators = [ validate_voucher ])
class Voucher(models.Model):
# Note: Model to maintain information about Voucher
vc = models.CharField (max_length = 25, verbose_name = 'Voucher ID',
help_text = 'Voucher Identifier')
ps = models.ForeignKey(Person, on_delete = models.CASCADE,
verbose_name = 'Person')
【问题讨论】:
标签: python sql django save models