我有“三个”模型:
class Book(models.Model):
title = models.CharField(max_length=200)
price = models.FloatField()
quantity = models.IntegerField()
class Operation(models.Model):
operation_type_choices = (
('sell', 'Sell'),
('donation', 'Donation'),
)
book = models.ManyToManyField(Book, through = 'BookOperation')
operation_type = models.CharField(max_length=50, choices=operation_type_choices)
class BookOperation(models.Model):
book = models.ForeignKey(Book)
operation = models.ForeignKey(Operation)
quantity = models.IntegerField()
我想知道我应该在什么模型中覆盖 save() 函数来管理操作何时“出售”,因此 book.quantity 应该降低,而当它是“捐赠”时它应该上升。