Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Q objects for subqueries with cross-references [RSS Feed]

#1 June 16, 2010 19:43:32

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Q objects for subqueries with cross-references


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/#inHowever, 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 16, 2010 22:29:40

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Q objects for subqueries with cross-references


On 16 kesä, 21:41, rt <rtoma...@gmail.com> wrote:
> 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.
>

Haven't tested this but this particular query should be doable with:

Post.objects.filter(tags__label__in=)

The thing inside parentheses could of course be represented with a Q
object.

For example I have a test db with bit different models:

class Person(models.Model):
name = models.CharField(max_length=200)


class Group(models.Model):
name = models.CharField(max_length=200)
persons = models.ManyToManyField(Person, through='Through')

class Through(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)

The query Groups.objects.filter(persons__name__in=) gives
this SQL:

'SELECT "autocomplete_group"."id", "autocomplete_group"."name" FROM
"autocomplete_group" INNER JOIN "autocomplete_through" ON
("autocomplete_group"."id" = "autocomplete_through"."group_id") INNER
JOIN "autocomplete_person" ON ("autocomplete_through"."person_id" =
"autocomplete_person"."id") WHERE "autocomplete_person"."name" IN
(anssi)'

--
Anssi

> 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Q objects for subqueries with cross-references [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support