Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 27, 2010 00:05:52

rahul j.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Foreign key Real world use case Q


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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 27, 2010 03:55:29

Emmanuel M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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 <jainwolver...@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-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support