Hi,
I have the following model definitions:
class Tag(models.Model):
name = models.CharField(max_length=16)
class Blog(models.Model):
name = models.CharField(max_length=16)
tags = models.ManyToManyField(Tag, blank=True)
I want to select blogs with more than one tags. Here is what I am doing (for two tags)-
# select blogs for python and java
blogs = Blog.objects.filter(tags__name='python').filter(tags__name='java')
This INNER joins Tag with Blog twice, each time filtering on the given name. This works well. Now I want to retrieve the matching tags. If it were a single join, I would have done-
blogs = blogs.annotate(tag=F('tags__name'))
Doing this still works, but only retrieves the last tag. How do I retrieve the tag name for each join, giving them different names? Something like-
# Below should give tag1 = 'python', tag2 = 'java'
blogs = blogs.annotate(tag1=?, tag2=?)
Thanks,
Ramashish