On Sep 6, 7:08 pm, Larry <yuelizh...@gmail.com> wrote:
> Hi everyone,
>
> I have the following three models: User, Item, User_Item, which are
> defined as follows
>
> def User(models.Model):
>     id = models.CharField(primary_key=True)
>
> def Item(models.Model):
>     id = models.CharField(primary_key=True)
>
> def User_Item(models.Model):
>     user = models.ForeignKey(User)
>     item = models.ForeignKey(Item)
>
> Now for example I want to find all the items that user JOHN is related
> to, i do
>     User_Item.objects.filter(user='JOHN')
> and I get a list of User_Item objects.
>
> The question is: WHAT is the easiest way to get a list of Item objects
> from the list
> of User_Item objects, i.e., instead of returning the user-item
> relation, we only return
> the items.
>
> Thanks,
> -Larry

Use the double-underscore syntax to traverse relations.
    Item.objects.filter(user_item__user='JOHN')
--
DR.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to