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',)) )
Radek paolo wrote: > Hi, I'm trying to do a simple cookbook application using Django, but > there is something unclear creating models. In fact, it's easy to > associate a category to the recipe via ForeignKey field, and it's easy > too associate more ingredients to a recipe via ManyToManyField. > However, each ingredient has an associated number of pieces (2 eggs) or > a weight (2tb olive oil), which I'm not able to express via the model. > Can you explain if it is possible and how? > > In cookbook/apps/recipes/models I have these three classes (they have > been stripped of minor details): > > class Ingredient(meta.Model): > fields = ( > meta.CharField('name', 'Name', maxlength=64), > meta.CharField('description', 'Description', maxlength=128, > blank=True), > ) > > class Category(meta.Model): > fields = ( > meta.CharField('name', 'Nome', maxlength=128), > meta.TextField('description', 'Description', blank=True), > ) > > class Recipe(meta.Model): > def get_diff_choices(): > DIFF_CHOICES = ( > ('01', 'Easy'), > ('02', 'Intermediate'), > ('03', 'Hard'), > ) > return DIFF_CHOICES > > fields = ( > meta.DateField('creation_date', 'Create', auto_now_add=True), > meta.CharField('name', 'Name', maxlength=128), > meta.ForeignKey(Category, name='category_id'), > meta.ManyToManyField(Ingredient, name='ingredient_id'), > meta.CharField('difficulty', 'Difficulty', maxlength=64, > choices=get_diff_choices()), > meta.TextField('description', 'Description'), > ) > > TIA