Recipe / Ingredient Model

179 views
Skip to first unread message

Shane Graber

unread,
Aug 2, 2007, 10:13:53 PM8/2/07
to django...@googlegroups.com
I'm trying to make a recipe/ingredient application as my road to
learning more about Django. Making a model of an ingredient and most
of the recipe are relatively straight forward based on the excellent
tutorials on the Django. However, I'm struggling with understanding
how to hook in multiple ingredients into a recipe. Here's how I have
my model setup:

http://dpaste.com/hold/15911/

I want to be able to add in new Ingredients from the /admin section of
Django instead of supplying it as a tuple hardcoded into my model.

When I create a Recipe, I need to have a series of rows below the
title, description, first, last name, and pub_date section where I can
select an Ingredient from a dropdown list and then to the right of the
ingredient needs to be an CharField where I input how much of each
component I need to add for that particular recipe. It would look
like:

Ingredient 1 2 tsp
Ingredient 2 1/2 cup
...

Each row starting with that Ingredient dropdown list followed by a CharField.

Any help on hooking this functionality into the model would be
*greatly* appreciated. Thanks!

Shane

Lucky B

unread,
Aug 2, 2007, 10:27:47 PM8/2/07
to Django users
You're missing just:
ingredients = models.ManyToManyField(Ingredient)

In your recipe model.

If you want a pretty interface in admin (if you're not using newforms-
admin) then:
ingredients = models.ManyToManyField('Ingredient',
filter_interface=models.HORIZONTAL)

Lucky B

unread,
Aug 2, 2007, 10:29:42 PM8/2/07
to Django users
ack, I missed a few things, the ingredient-quantity measure. not sure
how to do that. ignore my previous message.

John Danks

unread,
Aug 2, 2007, 10:34:40 PM8/2/07
to django...@googlegroups.com
On 8/2/07, Shane Graber <sgr...@gmail.com> wrote:
>
> I'm trying to make a recipe/ingredient application as my road to
> learning more about Django. Making a model of an ingredient and most
> of the recipe are relatively straight forward based on the excellent
> tutorials on the Django. However, I'm struggling with understanding
> how to hook in multiple ingredients into a recipe. Here's how I have
> my model setup:
>
> http://dpaste.com/hold/15911/
>
> I want to be able to add in new Ingredients from the /admin section of
> Django instead of supplying it as a tuple hardcoded into my model.

Not sure if this is the best way, but it works for me: http://dpaste.com/15913/

Basically ingredients like chicken, onions, etc are going to be in
lots of recipes but the quantity will be different in each. So make
one model for each type of ingredient, and another model to combine an
ingredient and a quantity. Then use edit_inline to make it convenient
to edit in the admin.

Lucky B

unread,
Aug 2, 2007, 10:49:04 PM8/2/07
to Django users
Ok, I've thought about it some more, and came up with this:
http://dpaste.com/hold/15915/
It's not quite what you asked for, for your idea of the interface
you're gonna have to either code a QuantityField type field to merge
both the quantity and unit, or just write views that accommodate the
middle-man model. I am pretty sure that's the only way to do it, but I
could be wrong.

Lucky B

unread,
Aug 2, 2007, 10:51:43 PM8/2/07
to Django users
God, 0/2, just do what John proposed, I am not thinking straight right
now, what I would give for an edit capability on my emails, I'll spend
all of tonight removing my foot from my mouth.

Shane Graber

unread,
Aug 2, 2007, 11:23:54 PM8/2/07
to django...@googlegroups.com
Excellent! This is exactly what I was looking for. Thanks for the help!

Shane

Shane Graber

unread,
Aug 2, 2007, 11:24:17 PM8/2/07
to django...@googlegroups.com
LOL, you don't know how often this happens to me. :P

Shane


--
Shane ∞ http://liquid.homelinux.org
---------
Bender: "Amy, you like cute things so I baked you a pony."
---------

http://www.reefs.org - Where reefkeeping begins on the internet.
http://www.advancedaquarist.com - High quality, free monthly publication for
the reef keeping hobbyist.
http://www.aquaristcourses.org - Distance learning courses for the
marine aquarist.

Reply all
Reply to author
Forward
0 new messages