Opened 12 years ago

Closed 2 years ago

#16979 closed New feature (wontfix)

adds Q objects for related fields.

Reported by: javier@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: Q, related_fields
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Inspired by this proposal by Johannes Dollinger: https://groups.google.com/forum/?hl=en#!topic/django-developers/lH5UhciGTBw, this quick (untested!) patch allows to use

is_blue = Q(color='blue')
Owner.objects.filter(thing=is_blue)

as a better alternative to:

Owner.objects.filter(thing__in=Thing.objects.filter(is_blue))

being better because it generates the same query as:

Owner.objects.filter(thing__color='blue')

Attachments (2)

df1.diff (1.7 KB ) - added by javier@… 12 years ago.
df1.2.diff (2.2 KB ) - added by javier@… 12 years ago.
improved, now it accepts Q(relfield=Q(...))

Download all attachments as: .zip

Change History (7)

by javier@…, 12 years ago

Attachment: df1.diff added

comment:1 by Carl Meyer, 12 years ago

Needs documentation: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Version: 1.3SVN

by javier@…, 12 years ago

Attachment: df1.2.diff added

improved, now it accepts Q(relfield=Q(...))

comment:2 by anonymous, 12 years ago

cleaned up, now there's a tree.Node._prefix() instance method, and the Q.__init__() constructor does the (..., relfield=qobj, ...) => (..., qobj._prefix(relfield+'__'), ...) transformation. No need to modify the db.query code.

comment:4 by Tim Graham, 10 years ago

Needs documentation: unset
Needs tests: unset
Patch needs improvement: set

The patch needs to be updated to apply cleanly to trunk and also needs a mention in the release notes. Since the PR has been polluted with some unrelated commits, here's the commit for this feature:

https://github.com/emulbreh/django/commit/34223e1bc864dd4f776b1094f388bf2fdbddfbd5

comment:5 by Mariusz Felisiak, 2 years ago

Resolution: wontfix
Status: newclosed
Triage Stage: AcceptedUnreviewed

Thanks for this ticket, however the proposed API (.filter(thing=Q(color='blue'))) is very confusing for me and I don't see any advantages over the existing API (.filter(thing__color='blue')). Please start a discussion on the DevelopersMailingList, where you'll reach a wider audience and see what other think, I'd like to reach a consensus before moving this forward.

Note: See TracTickets for help on using tickets.
Back to Top