我有两个模型:Product和Currency。class Product(models.Model): Currency = models.ManyToManyField( 'Currency', verbose_name=u'Currency', blank=True, null=True)class Currency(models.Model): Name = models.CharField(u'Currency Name', max_lenght=16) Sign = models.CharField(u'Currency Sign', max_lenght=4)将某些价值从模型。产品关联到模型。货币的最佳方法是什么?示例:model.Currency包含对象'USD','Euro','Krona'。model.Product包含与模型关联的对象“仙人掌”。货币“ USD”和“欧元”。如何设置“美元”和“欧元”的某些价格(价格)?我想这样的事情:Product.objects.get(Name='Cactus').get_price(Current.objects.get(Name='USD'))Product.objects.get(Name='Cactus').get_price(Current.objects.get(Name='Euro'))
1 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
class Product(models.Model):
currencies = models.ManyToManyField('Currency', through='Pricing', blank=True, null=True)
class Currency(models.Model):
name = models.CharField()
sign = models.CharField()
class Pricing(models.Model):
product = models.ForeignKey(Product)
currency = models.ForeignKey(Currency)
price = models.FloatField()
然后您可以使用类似
product = Product.objects.get(name='Cactus')
price = product.pricing_set.get(currency__name='USD')
添加回答
举报
0/150
提交
取消