Gio wrote:
> Hi,
> I searched about this subject and found only a few very old posts, so
> maybe there is a better solution now.
>
> As you may guess the model I'd like to code is similar to the Pizza -
> Toppings you know from the "Creating Models" documentation:
>
> class Topping(models.Model):
>     # ...
>
> class Pizza(models.Model):
>     # ...
>     toppings = models.ManyToManyField(Topping)
>
>
> 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 you should have

class Topping(models.Model):
    # ...

class Pizza(models.Model):
    # ...

class PizzaTopping(models.Model):
    amount = models.IntegerField()
    topping = models.ForeignKey(Topping)
    pizza = models.ForeignKey(Pizza)


The "PizzaTopping" class is really a more elaborate specification of the
many-to-many relationship, so you don't need the ManyToManyField
anymore. Your model has the problem that two pizzas can share
(Mozarella, 2)  - leading to problems if you modify one of them.

You will want to do the invocations for making (topping, pizza) unique
as well, and possibly do some magic so that trying to add another
Mozarella will increment the quantity instead.

Disclaimers: I am a novice Django user. The code is untested.

Update: Between posting and reposting from the correct mail address, I
saw the response from Michael. He is using "ManyToOneField" which I
don't see in my documentation, and keeps the ManyToManyField(Topping)
which seems strange to me. I believe you will be able to put together a
good solution from the two posts.

Nis



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to