On Thu, 2007-10-04 at 16:38 -0400, Malcolm Tredinnick wrote: > On Thu, 2007-10-04 at 20:08 +0000, bill wrote: > > Hi folks, > > I've reviewed the db api documentation page, and I'm pretty sure it > > didn't have an example that would help me with this request. I'd like > > something like the "IN" operator that can be used in a lookup on a > > field in a related model, but more like something that I guess could > > be called something like "ALL_IN". Example: > > Models: > > Article (title, pub_date, etc...) > > TaggedArticle (article_id, tag) > > > > One article can of course have multiple tags. What if I wanted to > > find Articles that have _all_ the tags in a particular set. Similar > > to IN: > > > > Article.objects.filter(taggedarticle__tag__in=['python', 'perl', > > 'java']) > > > > That would give me articles that have tags for python OR perl OR > > java. But what if I want a quick way to find all articles that have > > all three of those tags (they may have more tags, but they at least > > have all of those three.) > > > > I've learned about making my own Manager class, so that's definitely > > possible, but does there happen to be a built-in way? > > At the moment, this isn't possible. You can use the method in [1] to do > it right now, though. In the near future, once the queryset refactor is > finished, you'll be able to do this as > > A. > Article.objects.filter(tag='python').filter(tag='perl').filter(tag='java') > > or something like (I've possibly screwed up the syntax, but you get the > idea): > > Article.objects.filter(Q(tag='python'), Q(tag='perl'), Q(tag='java')) > > because many-to-many filters will successively restrict the result set. > However, that doesn't work right now because we don't do the correct > self-joins to form the right SQL. [By the way, it will also be possible > to construct this > > [1] http://www.pointy-stick.com/blog/2006/06/14/custom-sql-django/ > > Regards, > Malcolm
Thanks so much, Malcolm, your article was really spot-on. It's exactly what I want to do. Looking forward to the django enhancements, but in the meantime this customer manager approach looks great. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---