ManyToOne or ManyToMany Confusion

39 views
Skip to first unread message

aca...@gmail.com

unread,
Jan 16, 2013, 5:46:58 PM1/16/13
to django...@googlegroups.com
I'm trying to setup a relationship between two django models that is very similar to the model of a Pizza having many Toppings.  However, I need to make sure that if a Pizza is created then only one Pepperoni topping can be added.  The pizza could have the olive topping and onion topping, etc.  But only one of each.  Thanks for any help.

Lachlan Musicman

unread,
Jan 16, 2013, 5:53:19 PM1/16/13
to django...@googlegroups.com
Does Field.unique address your needs? ManyToOne may be the better solution...but without models to look at, it may require a validity check at save to confirm that each Pizza only had one of each type of Topping. 

Cheers
L.

On Thu, Jan 17, 2013 at 10:46 AM, aca...@gmail.com <aca...@gmail.com> wrote:
I'm trying to setup a relationship between two django models that is very similar to the model of a Pizza having many Toppings.  However, I need to make sure that if a Pizza is created then only one Pepperoni topping can be added.  The pizza could have the olive topping and onion topping, etc.  But only one of each.  Thanks for any help.



--
...we look at the present day through a rear-view mirror. This is something Marshall McLuhan said back in the Sixties, when the world was in the grip of authentic-seeming future narratives. He said, “We look at the present through a rear-view mirror. We march backwards into the future.”

http://www.warrenellis.com/?p=14314

Javier Guerra Giraldez

unread,
Jan 16, 2013, 6:12:49 PM1/16/13
to django...@googlegroups.com
On Wed, Jan 16, 2013 at 5:46 PM, aca...@gmail.com <aca...@gmail.com> wrote:
> The pizza could have the olive topping and onion topping, etc. But only one
> of each. Thanks for any help.

use an explicit 'through' table and set both foreignkeys as 'unique
together' on that table.


--
Javier

aca...@gmail.com

unread,
Jan 16, 2013, 7:07:15 PM1/16/13
to django...@googlegroups.com
Thanks.  I tried that technique and doing so only let me add one topping to the pizza.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages