【发布时间】:2015-11-30 17:52:38
【问题描述】:
class hotel(models.Model):
hotel_name = models.CharField(max_length=200)
class Sell(models.Model):
hotel = models.ForeignKey("hotel")
sell_name = models.CharField(max_length=200)
class Orderform(models.Model):
buy_choices = [(data.id+1, data.hotel_name) for data in hotel.objects.all()]
buy_hotel_id = models.IntegerField(choices=buy_choices,default=1)
class Selled(models.Model):
orderform = models.ForeignKey("Orderform")
sell_id = models.IntegerField()
sell_count = models.IntegerField(default=1)
我希望sell_id 使用选择
buy_choices = [(data.id+1, data.sell_name) for data in hotel.objects.get(id=buy_hotel_id).sell_set.all()]
sell_id = models.IntegerField(choices=sell_choices,default=1)
但我不知道如何获得buy_hotel_id
我想使用hotel.objects.get(id=buy_hotel_id)
例子
<hotel:1> have [<sell:banana>,<sell:apple>]
<hotel:2> have [<sell:grapes>,<sell:starfruit>]
如果buy_hotel_id 是1
想要结果[<sell:banana>,<sell:apple>]
我试试Sell.objects.all()
获取[<sell:banana>,<sell:apple>,<sell:grapes>,<sell:starfruit>]
这不是我想要的结果
【问题讨论】:
-
您想获得酒店的销售物品?问题很不清楚
-
+1 to taesu,此外,我不明白你为什么要这样做
data.id+1(这会给你错误的id,甚至是不存在的id),而且是动态的字段的选择比这要复杂一些。在表单中查看ForeignKey.limit_choices_to和 ModelChoiceField。 -
采纳 Paulo 的答案,
hotel.sell_set.filter(pk=1)不会为您的示例得到正确的结果吗?
标签: python django django-models