Monday, February 29, 2016

Re: Absolute beginner question -- recipes


> Thank you James for pointing me at how to do my queries with django; I can finally list my steps using:
>
> {% for step in recipe.steps.all %}
>
> within my template. However, given the model above I am still not sure how to access a single steps ingredients ... any ideas or suggestions on my current model?

Sure, just have another nested {% for ingredient in step.ingredients.all %}

You should also change the 'ingredient' field to 'ingredients' in your Step model (reflected in my loop above). Makes it easier to read and indicate that you'll need the .all in the template (or elsewhere) if the attribute name is plural. Otherwise you are querying using step.ingredient, and expecting a list of multiple ingredients back.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXjKnTgCpJcCT8hxP2gRgLOqA6DygkqT0WZr5Nqm8vQUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment