Steven Armstrong wrote:
> On 08/31/06 17:22, Anders Aagaard wrote:
> > Hi
> >
> > I've been learning to love django lately, but I can't quite wrap my
> > head around more advanced querysets.  I've listed my classes and their
> > sql tables on the bottom, as all problems are linked to those tables.
> >
> > First problem:
> > I have a class, Content, with a set of tags,  how can I filter for
> > multiple tags?
> > If I do:
> > list = Content.objects.filter(tag__id=1).filter(tag__id=2) it makes a
> > sql query asking content_content_tags for content_id's where tag_id = 1
> > and tag_id = 2, which will of course never match.
> >
>
> Something like this should work:
>
> mylist = Content.objects.filter(tag_id__range=(1,2,3,4,5))
>
> or:
>
> from django.db.models import Q
> mylist = Content.objects.filter(Q(tag_id=1) | Q(tag_id=2))

Your misunderstanding me, I dont want to browse objects with tag A OR
B, I want tag A AND B in this example.

What I thought would work:
#a = Content.objects
#b = a.filter(tags__id = 8)
#print b[0].tags.values()
[{'id': 3, 'name': 'TestTag'}]
#b.filter(tags__id = 3)
[]

Why doesn't the b.filter return b[0]?  Which does have that tag?

>
>
> > What I'd like to do is something like:
> > for tag in request.GET.getlist('tag')
> >     content = content.filter('match this tag too')
> >
> >
> >
> > Second problem:
> > I've looked at complex_filter for doing this and I can't quite figure
> > it out.
> > What if I wanted something like this:
> > content.objects.filter(id__id=1) | content.objects.filter(id__id=2)
> > But I needed to match a dynamic list of id's.  How could I do something
> > like
> >
> > content = content.objects.filter(content=example)
> > for match in id_list:
> >     content = content.objects.filter(id__id=match) #And here get a logical
> > OR comparison with the previus part of the loop.
> > So this would produce the same effect as:
> > content.objects.filter(id__id=id_list[0]) |
> > content.objects.filter(id__id=id_list[1])
> >
>
> from django.db.models import Q
> query = Q(tag_id=1)
> for the_tag_id in (2,5,6,8,9):
>      query = query | Q(tag_id=the_tag_id)
> mylist = Content.objects.filter(query)

Ahh!  So logical and clean, thanks a ton!

>
> Be careful with your use of the the double underscore __
> It has special meaning in djangoland.

Yeah, of course it'd be id=id_list[0], things tend to get tangled when
I try to sort this in my head ;)

> 
> Have a look at
> http://www.djangoproject.com/documentation/db_api/


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

Reply via email to