我試圖在基於Django的市場上爲每個交易創建交易歷史記錄。基於Django的市場,創建交易歷史記錄
我認爲跟蹤這些數據的最好方法是覆蓋save()函數並創建一個Transaction記錄。
class Transaction(models.Model):
item = models.ManyToManyField(Item, blank=True)
buyer = models.ManyToManyField(User, related_name='buyer')
seller = models.ManyToManyField(User, related_name='seller')
description = models.CharField(max_length=500)
purchase_date = models.DateField(auto_now_add=True)
value = models.DecimalField(max_digits=7, decimal_places=2)
def save(self, *args, **kwargs):
self.buyer.money+=self.value
self.seller.money-=self.value
super(Transaction, self).save(*args, **kwargs)
我對這一切都錯了嗎? Currenlty我得到...
'Transaction' instance needs to have a primary key value before a many-to-many relationship can be used.
這不僅是一個偉大的答案。我們可能是親戚。 – bdd 2010-11-29 04:16:16