2

我有一个具有 FK 定价的产品模型,因为一种产品可以包含多个价格。但我也希望能够从众多价格中选择哪一个应该是实际价格,因此我同时拥有价格(在产品模型中)和产品(在价格模型中)来实现这一点。考虑以下模型:

class Product(models.Model):
name = models.CharField()
price     = models.ForeignKey('Price', blank=True, null=True, related_name='Product')

class Price(models.Model):
amount = models.IntegerField()
product = models.ForeignKey('Product', related_name='product')

尽管我在过滤下拉菜单中的价格时遇到问题,但这工作正常。它给了我所有的价格,而不仅仅是与该产品相关的价格。试过了

limit_choices_to 

但这似乎不适用于动态值。

我也遇到过这个补丁: http ://code.djangoproject.com/ticket/2445

不知道最好的解决方案是什么。不胜感激,多谢指点!

4

1 回答 1

0

You could do:

prices = price.object_set_all(product='your product')

I left the tag 'your product' in because I don't recall if self will work in this situation. However I think this may be the correct approach.

You should not have the ForeignKey showing up in both models, you really only need it in Price. Then, your Product model can have a field called current_price which is based on a user selection.

于 2009-07-02T14:45:37.693 回答