<p>我有一些基本的一对多关系的模型类。例如,一本书有许多食谱,每个食谱都有许多成分:</p>
<pre><code>class Book(models.Model):
name = models.CharField(max_length=64)
class Recipe(models.Model):
book = models.ForeignKey(Book)
name = models.CharField(max_length=64)
class Ingredient(models.Model):
text = models.CharField(max_length=128)
recipe = models.ForeignKey(Recipe)
</code></pre>
<p>我想要一份单子,列出一本书所有食谱中的所有成分。用Python来表达这一点最好的方法是什么?</p>
<p>如果我用的是LINQ,我可能会这样写:</p>
<pre><code>var allIngredients = from recipe in book.Recipes
from ingredient in recipe.Ingredients
select ingredient;
</code></pre>