Malcolm, On Thu, 20 Jul 2006 16:56:22 +1000, Malcolm Tredinnick wrote:
>> > I think you want to use: >> > >> > Image.objects.filter(tags__id__in=[1,2]).distinct() >> >> Alas, this returns all the images with tag1 OR tag2. I'm looking for >> images with tag1 AND tag2. > > Here's a solution that I prepared earlier (it talks about finding all > groups containing a subset of people, but it's the same problem): > > http://www.pointy-stick.com/blog/2006/06/14/custom-sql-django/ Wow, thanks. What a nice writeup! I assume you don't mind me using your code in whatever way I choose? As part of a baby-killing machine as it happens ;-) (not really of course) One comment about the code, it only seems to work if the list of people is 2 or more. There seems to be an error with the tuple()'ed bit in the query if the list is of length 1. I just special cased len() == 1 to make this work in my code. A second more general note about the Django ORM. First off, excuse me if I'm talking through my ass, I have almost no experience with DBs, but... The Django ORM API seems to imply that chaining filter() (i.e. queryset.filter(stuffA).filter(stuffB)...filter(stuffZ)) is the logical equivalent of ANDing together the conditions stuffA ... stuffZ. If this is technically infeasible to do on m2m relations I think it would be better for the ORM to raise an error of some sort rather than just returning an empty set. Also, would it be feasible for a query like this with multiple m2m conditions to realise what's going on and to call the code described on your blog, or am I just dreaming? Regards Neilen > > Regards, > Malcolm > > > > -- you know its kind of tragic we live in the new world but we've lost the magic -- Battery 9 (www.battery9.co.za) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---