Hi,

> Just give an amount field to the Ingredient:
> 
> class Ingredient(meta.Model):
>     fields = (
>         meta.CharField('name', 'Name', maxlength=64),
>         meta.CharField('description', 'Description', maxlength=128,
> blank=True),
>         meta.FloatField('amount','Amount', max_digits=5,
> decimal_places=2),
>         meta.CharField('unit', 'Unit', maxlength=32,
> choices=('kg','lb','ps','l',))
>     )
> 

This won't work because different recipes will have different amounts.

Radek's suggestion is the way to go. But make sure you re-visit your
Recipe class to remove the ManytoMany attrib pointing to Ingredients.
The problem as you have rightly identified is that the intermediate
table (automatically created by django when a ManytoMany field is
used) provides for only foreign keys and does not provide for
additional data fields. Therefore we have to step in and create the
intermediate table with the extra fields via the RecipeIngridient
class.

Also, once you have become comfortable with this approach you could
add some code such as _get_ingredients (in the Recipe class) and
_get_recipes (in the Ingredients class) for easy access to the related
data.

Hope that helps.

Deepak

Reply via email to