Opened 16 years ago
Closed 16 years ago
#11899 closed (invalid)
QuerySet - filter through related objects
| Reported by: | fgrzadkowski | Owned by: | nobody |
|---|---|---|---|
| Component: | Documentation | Version: | 1.1 |
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Lets say we have models like:
class A(models.Model):
pass
class B(models.Model):
t = models.CharField(max_length=100)
a = models.ForeignKey('A')
I've found out, that the following query is working:
A.objects.filter(b__t='1').filter(b__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 looks 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
So we have two thigns which are not documented (or I can't find it):
- possibility to filter like above
- if we set related_name="bs" in B for field a, then it affect not only foreignkey manager in A, but also filter function (
.filter(bs__t='1')
Or maybe its all not official features? But if not then it should be, because it's very useful :)
Note:
See TracTickets
for help on using tickets.