Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#1133 closed enhancement (fixed)

[patch] Allow use of *args as well as **kwargs for DB queries

Reported by: freakboy@… Owned by: adrian
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: magic-removal, argument, args, kwargs, Q
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This patch is a backwards-compatible extension to the handling of query parameters. It is against the magic-removal branch.

Without this patch, query arguments must be provided as kwargs to get_list, etc. If complex AND/OR queries are required, they must be wrapped in Q() objects, and the complete complex query is provided as a complex= kwarg.

This patch modifies the get_* calls on the Manager to allow the use of *args, as well as kwargs in queries.

This means that Q() objects can be used directly as a query argument. The complex= kwarg continues to work as before. Queries provided as Q() objects are ANDed/merged in the same way that kwargs are handled.

For example, using the new syntax, the query:

polls.get_object(
   complex=
       (Q(question__startswith='Who') &
           (Q(pub_date__exact=date(2005, 5, 2)) |
            Q(pub_date__exact=date(2005, 5, 6)))
)

could also be phrased as:

polls.get_object(
   Q(question__startswith='Who'),
   Q(pub_date__exact=date(2005, 5, 2)) | Q(pub_date__exact=date(2005, 5, 6))
)

or, combining the two approaches,

polls.get_object(
   Q(pub_date__exact=date(2005, 5, 2)) | Q(pub_date__exact=date(2005, 5, 6)),
   question__startswith='Who'
)

NOTE 1: Some query orders become significant, as *args must precede the kwargs. This is the only drawback to this patch, but IMHO, it is something that could be handled through adequate documentation.

NOTE 2: Strictly, queries are not restricted to Q() objects; any object with a get_sql method can be provided as a query. A check for the existence of the get_sql method is made before the query is evaluated.

In addition to implementation code, this patch includes test code, as part of the OR testing package (i.e., the same place complex= is tested)

This patch introduces no incompatibilies in practice - however, in theory, there are some edge case incompatibilities in the get_in_bulk and get_date_list methods. These methods are affected because they already have (and use) *args in an unusual fashion. This patch preserves the important behaviour of these arguments, but the handling of some unusual (and probably erroneous) calling styles may be affected.

Attachments (3)

query_args.patch (9.1 KB) - added by freakboy@… 10 years ago.
Patch to add *args handling to DB queries
query_args.r1799.patch (9.3 KB) - added by freakboy@… 10 years ago.
Updated patch after merge to r1799
query_args.r1799.v2.patch (8.8 KB) - added by freakboy@… 10 years ago.
Updated patch after merge to r1799

Download all attachments as: .zip

Change History (5)

Changed 10 years ago by freakboy@…

Patch to add *args handling to DB queries

Changed 10 years ago by freakboy@…

Updated patch after merge to r1799

Changed 10 years ago by freakboy@…

Updated patch after merge to r1799

comment:1 Changed 10 years ago by freakboy@…

Ignore the first two patches; the first one is deprecated as of r1799; the second contains some debug code I forgot to remove before diffing. query_args.r1799.v2.patch is the complete, up-to-date patch.

comment:2 Changed 10 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [1884]) magic-removal: Fixed #1133 -- Added ability to use Q objects as args
in DB lookup queries.

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