On Tue, 2006-08-29 at 14:05 +0000, [EMAIL PROTECTED] wrote:
> > > Yes, you are right.  I was not thinking straight.
> 
> Not a problem. Help is always appreciated!
> 
> > > Anyone know what the
> > > best method for performing this in SQL would be?  Select all posts for
> > > each tag and use intersect?
> >
> > With ManyToMany relationships, you have to think of chasing the
> > relationship backwards. Instead of finding the posts with a given tag,
> > start with the tag and find the related posts:
> >
> > >>> from danet.blog.models import Post, Tag
> > >>> t = Tag.objects.get(pk='programming')
> > >>> t.post_set.all()
> > [<Post: ASP.NET 2.0>, <Post: Code Highlighting>]
> > >>>
> 
> That works easily when you're just looking up one Tag. What I'm trying
> to figure out is the best way to search for multiple tags and return
> only the Posts common to all of those tags:

I am really close to finishing the rewrite work necessary to make this
easy. It's a bug that it doesn't work already. You should be able to
filter using

        Post.objects.filter(tag = 'django').filter(tag = 'python')
        
and have it return Post instances that have both 'django' and
'python' (and possibly other tags) associated with them. It sounds like
this is what you are after. Right now, like I said, it's a bug that this
doesn't already work (since we say that concatenating filters should act
like "and"-ing them together). I'm going to get back to doing some
Django core dev work this week and this is top of my list.

In the interim, you might like to try this solution, which also works,
but is a little fiddlier:
http://www.pointy-stick.com/blog/2006/06/14/custom-sql-django/

(There have been other solutions to the same problem posted on this
list, too).

Cheers,
Malcolm



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