Sunday, December 26, 2010

Re: Foreign key Real world use case Q

Wrong model definition!!!
you need a many to many or a one to many relationship.
In your current model, you have one to one relationship.

class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)

http://www.djangoproject.com/documentation/models/many_to_many/

In you particular case, a one_to_many relationship may be more
appropriate, in which case
it should be the item which has the foreign key.

class Item():
desc = ...
menu = models.ForeignKey(Menu)

and yes, you need to assign each item/dish to a menu type!
mymenu = Menu()
item=Item(desc="sdfsdf", menu=mymenu)
or
item.menu = mymenu

Good luck,

--
Emmanuel Mayssat

On Sun, Dec 26, 2010 at 4:05 PM, rahul jain <jainwolverine@gmail.com> wrote:
> Foriegn Key Use case
>
> Class Item():
>      desc = models.StringFrield()
>
> Class Menu():
>       type = models.StringField(default = "Italian")       ///cuisine type
>       item_list = models.ForiegnKey(Item)
>
> So for each cuisine I have some list of items
>
> for ex:
>
> menu type 1 for ex  have 5  items (item 1, item2, item 3, item4, item5)
>
> Creating items is easy
>
> item = Item()
> item.desc = "item1"
> item.save()
>
> item = Item()
> item.desc = "item 2"
> item.save()
>
> so on......
>
> Now I have to create just a single menu item
>
> menu = Menu()
> menu.type ="Italian"
> menu.item_list = ???   ///i am not sure what should be this , should
> it be assigned to any one of them
>
> Then how to retreieve the list of items for a menu ex: here Italian
>
> Thanks.
>
> --RJ
>
> --
> 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 django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment