Django

Code

Ticket #1133 (closed: fixed)

Opened 3 years ago

Last modified 1 year ago

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

Reported by: freakboy@iinet.net.au Assigned to: adrian
Milestone: Component: Database wrapper
Version: Keywords: magic-removal, argument, args, kwargs, Q
Cc: Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

query_args.patch (9.1 kB) - added by freakboy@iinet.net.au on 12/29/05 01:53:41.
Patch to add *args handling to DB queries
query_args.r1799.patch (9.3 kB) - added by freakboy@iinet.net.au on 12/29/05 21:16:46.
Updated patch after merge to r1799
query_args.r1799.v2.patch (8.8 kB) - added by freakboy@iinet.net.au on 12/29/05 21:18:52.
Updated patch after merge to r1799

Change History

12/29/05 01:53:41 changed by freakboy@iinet.net.au

  • attachment query_args.patch added.

Patch to add *args handling to DB queries

12/29/05 21:16:46 changed by freakboy@iinet.net.au

  • attachment query_args.r1799.patch added.

Updated patch after merge to r1799

12/29/05 21:18:52 changed by freakboy@iinet.net.au

  • attachment query_args.r1799.v2.patch added.

Updated patch after merge to r1799

12/29/05 21:22:59 changed by freakboy@iinet.net.au

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.

01/09/06 05:02:06 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #1133 ([patch] Allow use of *args as well as **kwargs for DB queries)




Change Properties
Action