On 7/20/06, Neilen Marais <[EMAIL PROTECTED]> wrote: > > Hi > > A photo-app I'm working on has tags on an image defined as a many to many > relationship: > > class Image(models.Model): > filename = models.CharField(maxlength=255) > description = models.TextField() > tags = models.ManyToManyField(Tag) > > Given several tags: TagA, TagB, ..., I'm trying to find images that > contain each of the tags. > > I try this: > > In [73]:Image.objects.filter(tags__id=1).filter(tags__id=2) > Out[73]:[] >
I think you want to use: Image.objects.filter(tags__id__in=[1,2]).distinct() > However, I know images with both of those tags exist, eg: > > In [73]:i = Image.objects.filter(tags__id=1)[0] > > In [74]:i > Out[74]:<Image: 2004-09-23++14-10-03_++_dsc_5994.jpg> > > In [75]:[tag.id for tag in i.tags.all()] > Out[75]:[1, 2, 3, 17, 170, 259, 362, 387, 414, 647, 681, 810] > > How can I find the image(s) with a given set of tags? > > Thanks > Neilen > > -- > 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 -~----------~----~----~----~------~----~------~--~---