我有以下两种型号:
class Model_ItemName(models.Model):
item_name = models.CharField(max_length = 100, null = True, blank = False)
def __unicode__(self):
return "Item name: {}, current quantity: {}".format(self.item_name, ...) # How to include the **sum** of all of its "quantity" from "Model_Transaction" here
class Model_Transaction(models.Model):
item_name = models.ForeignKey(Model_ItemName)
quantity = models.FloatField(null = True, blank = True)
def __unicode__(self):
return self.item_name
当从CreateView的Model_Transaction模型中选择外键下拉菜单时,我们如何不仅显示“ item_name”,还显示其所有相关“数量”的总和?因此,例如在下拉列表中,选择如下所示:
Item name: ABC, current quantity: 213 # (200 + 15 - 2)
...
Item name: XYZ, current quantity: 91 # (87 + 4)
通过这种方式,用户将在添加/减去该项目之前知道该选定项目的总数量。
我欢迎任何建议/解决方案。谢谢