我有一个具有 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
不知道最好的解决方案是什么。不胜感激,多谢指点!