I might be wrong here, but this is what I understand:
Let's say you have something like this:
class Recipe(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
class Step(models.Model):
step_number = models.IntegerField()
description = models.TextField()
recipe = models.ForeignKey(Recipe)
now, if I do the following in the shell:
recipe = Recipe.objects.get(id=1)
recipe.steps
wouldn't this give an error?
To get the step, I could do something like
recipe.step_set
but I don't think I'll be able to do recipe.steps
On Monday, February 29, 2016 at 8:08:35 AM UTC+5:30, James Schneider wrote:
>
>
>
> On Sun, Feb 28, 2016 at 11:14 AM, Simon Gunacker <[email protected]
> <javascript:>> 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/3e8828f2-a104-4855-a728-58c9b8362347%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.