Code

Opened 3 years ago

Last modified 2 months ago

#16979 new New feature

adds Q objects for related fields.

Reported by: javier@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: Q, related_fields
Cc: Triage Stage: Accepted
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@… 3 years ago.
df1.2.diff (2.2 KB) - added by javier@… 3 years ago.
improved, now it accepts Q(relfield=Q(...))

Download all attachments as: .zip

Change History (6)

Changed 3 years ago by javier@…

comment:1 Changed 3 years ago by carljm

  • Needs documentation set
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.3 to SVN

Changed 3 years ago by javier@…

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

comment:2 Changed 3 years ago by anonymous

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 Changed 2 months ago by timo

  • 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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.