【发布时间】:2018-07-27 20:17:07
【问题描述】:
我正在尝试研究 models.cascade 语句的正确用法。我有两个模型 Cart 和 Entry。如果我删除一个条目,则购物车条目上的删除不会更新。我已经通过管理界面检查了这一点。我的models.py如下:
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
count = models.PositiveIntegerField(default=0)
total = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return "Cart:{} User:{} Items:{} Total:£{}".format(self.id, self.user, self.count, self.total)
class Entry(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
cart = models.ForeignKey(Cart, null=True, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=0)
【问题讨论】:
-
model.cascade 实际上并没有为我摆脱记录。所以如果我删除一个条目,购物车不会更新。
标签: python django django-models