agreed with Malcom on this one:

you should be splitting the url string at the top (removing the last blank entry) to get the array of tags.

from there you can just iterate over the tags array until you hit the end in a for loop.

then in that for loop, just call a filter on the queryset, using the current tag as the param in the filter call.

(note, I am not near a django install at the moment, so this is all untested, ymmv)

tags = url.split("/")[:-1]

posts = Post.objects.all()

for tag in tags:
posts.filter(posttag = tag)

return render_to_response("blog/tags.html", {'posts': posts})

if the filter call isn't working correctly, that's another story :)

good luck!

-C

On 8/29/06, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:

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