QuerySet - filter through related objects

5 views
Skip to first unread message

filippo

unread,
Sep 17, 2009, 7:39:45 AM9/17/09
to Django users
Hi,

Lets say I have models like:

class A(models.Model):
pass

class B(models.Model):
t = models.CharField(max_length=100)
a = models.ForeignKey('A', related_name="bs")

I've found out, that the following query is working:

A.objects.filter(bs__t='1').filter(bs__t='2')

which returns those As that have at least one B with t='1' AND at
least one B with t='2'. The underlying query lokk like this:

SELECT "search_test_a"."id" FROM "search_test_a" INNER JOIN
"search_test_b" ON ("search_test_a"."id" = "search_test_b"."a_id")
INNER JOIN "search_test_b" T3 ON ("search_test_a"."id" = T3."a_id")
WHERE ("search_test_b"."t" = E'1' AND T3."t" = E'2' ) LIMIT 21

Now my question:

Is it an official feature? I can't find it anywhere in
documentation...

regards,
Filip

mrts

unread,
Sep 17, 2009, 9:45:07 AM9/17/09
to Django users
On Sep 17, 2:39 pm, filippo <fgrzadkow...@gmail.com> wrote:
> Is it an official feature? I can't find it anywhere in
> documentation...

http://docs.djangoproject.com/en/dev/topics/db/queries/#chaining-filters
Reply all
Reply to author
Forward
0 new messages