我有两种型号:产品和货币。
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包含一个对象'Cactus',它与models.Currency'USD'和'Euro'相关联
如何设置一些价值(价格)为“美元”和“欧元”?
我想知道这样的事情:
Product.objects.get(Name='Cactus').get_price(Current.objects.get(Name='USD'))
Product.objects.get(Name='Cactus').get_price(Current.objects.get(Name='Euro'))
提前致谢!
答案 0 :(得分:1)
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')