> And what if I need to say that I can have two or three times the same > topping on my pizza? Something like twice mozzarella cheese and 3 > times green olives topping? > > I though about an intermediary class and indeed this is the same > solution found in those old posts mentioned before: > > class Topping(models.Model): > # ... > > class ToppingAndQuantity(models.Model): > amount = models.IntegerField() > topping = models.ForeignKey(Topping) > > class Pizza(models.Model): > # ... > toppings = models.ManyToManyField(ToppingAndQuantity) > > I think it's ugly. > Can you suggest me a better solution? > It's that intermediary class really needed?
I believe this is the best way to do it. However, this generally gets composed as class Pizza(Model): #don't reference toppings here class ToppingAndQuantity(Model): pizza = ForeignKey(Pizza) topping = ForeignKey(Topping) amount = PositiveIntegerField() The information you want (the quantity) is associated with the join between a pizza and its toppings. The ManyToMany would make a situation where you could have one pizza that has both 2x Cheese and 4x Cheese. I suspect that you want just one topping per pizza, but a quantity associated with that one topping. The ManyToMany is a nice shortcut for basically creating this intermediate join model with only two FK fields in it (pizza and topping) with a little syntactic sugar around them. With the above, you should be able to do things somewhat like p = Pizza.objects.get(id=1) for topping in p.toppingandquantity_set: print "%s x%i" % (p.topping, p.amount) -tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---