On Sun, Feb 28, 2016 at 11:45 PM, Rahul Gandhi <rahul.rahulg...@gmail.com>
wrote:

> 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
>
>
Given that model, you're right, sorry about that. My brain was moving
faster than my fingers. All of my mentions of recipe.steps were meant as a
reference to recipe.step_set. In my head I had already set the related_name
to 'steps', like so:

recipe = models.ForeignKey(Recipe, related_name='steps')

This way you can use recipe.steps rather than recipe.step_set. Both are
equivalent, but the custom related_name is easier to track for a human. The
relation does act like its own manager though, so you would actually have
to say recipe.steps.all() to get all of the steps (since a separate query
is used to pull all of the steps). The template code would also have to be
adjusted to include the .all (no parenthesis there). Also be sure to
determine whether or not you should use prefetch_related for your list
views (
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#prefetch-related
).

However, this model does match what I assumed you had. I'm also assuming
that you have a M2M relationship with Ingredient with a custom 'through'
table so that you can match steps with ingredients and store other things
like measurements on that relation?

I would make a suggestion, though. Instead of using a 'step_number', you
should look at using django-ordered-model instead. It does a very good job
of keeping things, well, ordered. It also has other functionality like
performing a renumber automatically if you decide that step 2 should
actually be step 18 (and renumbers all of the other steps appropriately).
The 'order' that is uses is zero-based, but you can add a simple property
step_number that returns self.order + 1 (and sets the order correctly if
you are renumbering by step). You'll also need to implement their
'order_with_respect_to', probably point it at the recipe field.

https://github.com/bfirsh/django-ordered-model

-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+unsubscr...@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%2BciUw-Yqv3cK-M_n%2B%2BqutU8BDzbbYGhqJYxS3M%3DmX%2BqKYwA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to