擅长:python、mysql、java
<p>要打印每个配方及其成分:</p>
<pre><code>mybook = Book.objects.get(name="Jason's Cookbook")
for recipe in mybook.recipe_set.all():
print recipe.name
for ingredient in recipe.ingredients:
print ingredient.text
</code></pre>
<p>如果你只想得到一份所有成分对象的列表:</p>
<pre><code>mybook = Book.objects.get(name="Jason's Cookbook")
ingredient_list = []
for recipe in mybook.recipe_set.all():
for ingredient in recipe.ingredients:
ingredient_list.append(ingredient)
</code></pre>
<p><a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#related-objects" rel="nofollow noreferrer">Documentation</a>。</p>