我有以下型号:
FreightItem
我们说这是一个商业规则,所有指向货运项目的订单商品都属于同一商家,换句话说,商家只会生成一个货运商品。
在我现有的class FreightItem(Model):
amount = DecimalField()
@property
def merchant(self):
return self.orderitems_set.first().merchant
课程中,为了获取商家,我使用属性返回第一个订单商品来访问商家,如下所示:
FreightItem
当我读到这段代码时,脑子里有些东西让我大喊大叫它不对。但另一种选择是将商家字段添加到class FreightItem(Model):
merchant = ForeignKey(Merchant)
amount = DecimalField()
模型中:
module SkApp.Core {...
但这在表格中似乎是多余的(非规范化的)。
你们更喜欢哪种方式?
答案 0 :(得分:0)
.first()
如果您的FreightItem
从未属于OrderItem
,则class OrderItem(Model):
freight_item = ForeignKey(FreightItem)
@property
def merchant(self):
return self.freight_item.merchant
class FreightItem(Model):
amount = DecimalField()
merchant = ForeignKey(Merchant)
调用将失败(将返回无)。
为什么不这样做:
{{1}}