我需要獲取給定菜單的項目名稱和數量的列表。我如何查詢並返回具有所需列的QuerySet?瀏覽多對多的關係Django
我只得到菜單列表,但仍然無法獲取數量。我應該怎麼做多個查詢,或者嘗試使用原始的SQL
class Item(models.Model):
name = models.CharField(max_length=20)
class Meals(models.Model):
name = models.CharField(max_length=50)
ingredients = models.ManyToManyField(Item, through='MealRecipe')
class Menu(models.Model):
name = models.CharField(max_length=50)
meals = models.ManyToManyField(Meals,through='CompMenu')
class CompMenu(models.Model):
TYPE_COMP = (
('B', 'Breakfast'),
('L', 'Lunch'),
('D', 'Dinner')
)
menu = models.ForeignKey(Menu)
meal = models.ForeignKey(Meals)
type = models.CharField(max_length=1, choices=TYPE_COMP)
class MealRecipe(models.Model):
meal = models.ForeignKey(Meal)
item = models.ForeignKey(Item)
qty = models.IntegerField()
謝謝,我怎樣才能得到與給定菜單相關的給定餐飲清單中的物品清單。我應該使用多個查詢嗎? – gaccep
我得到的對象有沒有屬性'名稱' – gaccep
我希望我現在正確理解你的問題,並作出適當的修改。 – TitanFighter