Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Multiple conditions on same field in JOIN over three models [RSS Feed]

#1 Dec. 17, 2010 18:20:41

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

Multiple conditions on same field in JOIN over three models


Hello!

Assuming the following data model:

+------+ +------+ +-----+
| Blog | has many (1:n) | Post | tagged with (n:m) | Tag |
+------+ +------+ +-----+I want to filter out all blogs that have a post tagged with "foo" and"bar" (the same post satisfying *both* conditions).How would that query look like? Intuitively I'd say

Blog.objects.filter(post__tags__name="foo",
post__tags__name="bar")but obviously that's a SyntaxError because of the repeated keywordargument to filter(). I also tried Q() objects but that generates sometotally unrelated queries.The docs only have an example with two conditions against differentfields (in topics/db/queries:"Spanning multi-valued relationships").Thanks!
Jonas

--
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 Dec. 17, 2010 19:21:37

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

Multiple conditions on same field in JOIN over three models


On Fri, Dec 17, 2010 at 1:20 PM, Jonas H. <jo...@lophus.org> wrote:
> Blog.objects.filter(post__tags__name="foo",
>                        post__tags__name="bar")

untested:

Blog.objects.filter(post__tags__name="foo").filter(post__tags__name="bar")

--
Javier

--
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

#3 Dec. 17, 2010 19:30:21

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

Multiple conditions on same field in JOIN over three models


On 12/17/2010 08:21 PM, Javier Guerra Giraldez wrote:Blog.objects.filter(post__tags__name="foo").filter(post__tags__name="bar")That's "get all blogs that have at least one post tagged with 'foo' andone post tagged with 'bar'", not requiring that *one post* matches*both* conditions.--
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

#4 Dec. 17, 2010 19:39:23

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Multiple conditions on same field in JOIN over three models


On 17 December 2010 19:20, Jonas H. <jo...@lophus.org> wrote:
> Hello!
>
> Assuming the following data model:
>
> +------+                +------+                   +-----+
> | Blog | has many (1:n) | Post | tagged with (n:m) | Tag |
> +------+                +------+                   +-----+
>
> I want to filter out all blogs that have a post tagged with "foo" and "bar"
> (the same post satisfying *both* conditions).
>
> How would that query look like? Intuitively I'd say
>
>    Blog.objects.filter(post__tags__name="foo",
>                        post__tags__name="bar")
>
> but obviously that's a SyntaxError because of the repeated keyword argument
> to filter(). I also tried Q() objects but that generates some totally
> unrelated queries.

In : print Blog.objects.filter(Q(post__tags__name="foo") &
Q(post__tags__name="bar")).query

SELECT "simple_blog"."id"
FROM "simple_blog"
INNER JOIN "simple_post" ON ("simple_blog"."id" =
"simple_post"."blog_id")
INNER JOIN "simple_post_tags" ON ("simple_post"."id" =
"simple_post_tags"."post_id")
INNER JOIN "simple_tag" ON ("simple_post_tags"."tag_id" =
"simple_tag"."id")
WHERE ("simple_tag"."name" = foo AND "simple_tag"."name" = bar )

Looks related. It's exactly what you specified, but conditions you
gave are just impossible to satisfy.

Your task is a bit more complicated to do in SQL:

Blog.objects.filter(post_tags__name__in=).annotate(match_count=Count('post__tags')).filter(match_count=2)

This should give you the right results.

--
Łukasz Rekucki

--
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

#5 Dec. 17, 2010 21:40:21

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

Multiple conditions on same field in JOIN over three models


On 12/17/2010 08:38 PM, Łukasz Rekucki wrote:Blog.objects.filter(post_tags__name__in=).annotate(match_count=Count('post__tags')).filter(match_count=2)

This should give you the right results.Neat! And blazingly fast, too, I guess!

Thank you very much, anyway :-)

Jonas

--
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
  • » Multiple conditions on same field in JOIN over three models [RSS Feed]

Board footer

Moderator control

Enjoy the 12th of December
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