Django-模型结构

问题描述 投票:1回答:1

我是Django的新手,在我的模型结构中遇到这种情况:

我有一个模型Item代表很多待售的物品。

某些项目具有tag。有些没有。

拥有tag的人将享有相同的percentage折扣:用户可以选择的五个选项(5%,10%,15%,20%,25%);

当管理员/用户将折扣从5%提高到15%时,所有带有标签的商品都将具有相同的15%折扣。

[我不确定是否应该设置布尔值tag字段,然后设置percentage的另一个字段,还是托管折扣percentage选择的另一个模型,还是应该只为percentage设置标签选择的IntegerField ]?但是如何使所有具有相同标签的项目保持同步?有没有一种消耗最少资源的方法?

python django database model structure
1个回答
0
投票
from decimal import Decimal class Tag(models.Model): percentage = models.DecimalField(max_digits=2, decimal_places=2) class Item(models.Model): # … discount_tag = models.ForeignKey(Tag, on_delete=models.PROTECT, null=True) @property def discount(self): if self.discount_tag is not None: return self.discount_tag.percentage return Decimal()

[因此,我们可以在此添加例如@property,以检查Item是否具有相关标签,如果有,则返回该percentagediscount_tag。否则,它返回Decimal('0')

© www.soinside.com 2019 - 2024. All rights reserved.