Hi, I am having a hard time with Q objects based on subqueries. It's a part of a linguistic corpus analysis query builder (hence Q objects as this is how I parse and translate arbitrary queries into Django) -- the user can search 'posts' that meet complex criteria. Anyway, it's all working fine and dandy with the exception of querying post tags (there is a many-to-many relationship between posts and tags).
A simplified version of my models.py: --- BEGIN --- class Tag(models.Model): label = models.CharField(max_length=255) class Post(models.Model): tags = models.ManyToManyField(Tag, through='TagsForPost') class TagsForPost(models.Model): tag = models.ForeignKey(Tag) post = models.ForeignKey(Post) --- END --- The query parser iterates over the query expression and for every criterion (=smallest subexpression) it adds a Q object. Let's say I have this query: 'Tag' 'is' "tagA" AND 'Tag' 'is' "tagB" which should yield all posts that are tagged with both tagA and tagB. So I feel I need something that will generate IN operator in SQL, with the condition set being the result of a subquery fetching all tags for a post. This seems to be handled nicely by Django: http://docs.djangoproject.com/en/dev/ref/models/querysets/#in However, since I am looking for posts, I need to cross-reference the post in the inner query with the post in the outer query (on the SQL level at least). I guess matching posts by primary key is the way to go here. However, I can't quite wrap my mind around the Django syntax here. Can anyone help me with getting the proper Q object that will do the trick? Let's assume that for every iteration the tag name (string) is held in a variable called 'value'. As an example, for conditions for searching for posts starting with a given text I do: result = Q(body_anonymized__istartswith=value) ('body' was originally a part of Post above in models.py) Thanks in advance! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.