On Sun, Feb 28, 2016 at 11:14 AM, Simon Gunacker <[email protected]>
wrote:

> Dear Django Community,
>
> as a first django project I tried to write a site to manage cooking
> recipes. My model is quite simple:
>
> * 1 recipe has n steps
> * 1 step has a description and n ingredients, each of a certain amount
> defined by a certain unit
>
> all in all, I have 5 tables (Recipe, Step, Ingredient, Unit,
> StepIngredient).
>
> After reading through some documentation and trying around a little bit, I
> got my model and my admin site (I had to install django-nested-inline for
> the admin site to fit my expectations).
>
> Now I am struggeling with my views. There is a simple index view wich
> basically lists all recipes:
>
> class IndexView(generic.ListView):
>     context_object_name = 'recipes_list'
>
>     def get_queryset(self):
>         return Recipe.objects.all()
>
>
You can actually get rid of the get_queryset() override entirely here by
properly setting the default view model to Recipe.



> The hard part is setting up the details view. It is supposed to show the
> recipes name as a title and then list all the required ingredients and all
> the required steps. It tried something like:
>
> def detail(request, pk):
>     recipe = Recipe.objects.get(id=pk)
>     recipe['steps'] = Steps.objects.filter(recipe_id=pk)
>     template = loader.get_template('recipe/recipe_detail.html')
>     context = {
>         'recipe': recipe,
>     }
>     return HttpResponse(template.render(context, request))
>
> but it seems I am not allowed to modify the recipes object. Any ideas how
> to pass all the data which belongs to recipe to the template?
>
>
Whoa...why are you manually setting recipe['steps']? Isn't there already a
relationship between Recipe (the model) and Step (Step should have a FK
back to Recipe)? You should not need an explicit second call to
Step.objects. If that call is necessary, then I would be highly suspicious
that your models are not defined properly (sanely).

I would remove that second line where you set recipe['steps'] entirely. If
that breaks something, your models are probably incorrect.

Assuming you run just the first line (recipe = Recipe.objects.get(id=pk)),
you should be able to do the following in the Django shell:

recipe.steps # return a list of all of the steps associated with that recipe

recipe.steps[2].ingredients # return a list of ingredients for what is
presumably step 3 of the recipe

There shouldn't be any need for manually querying for the steps or their
ingredients after the original Recipe.objects.get() call.

-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 [email protected].
To post to this group, send email to [email protected].
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%2BciWUCC3QfHdzCCEE8o9H8i%2BX1XoyFyf%2BOhBhF8BMh%2Bv5%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to