Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#214 closed enhancement (fixed)

[patch] Allow selecting individual fields in SQL SELECT statements via the models API

Reported by: rmunn@… Owned by: adrian
Component: Metasystem Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

A common request on IRC recently has been "How do I do SELECT DISTINCT field FROM table"? The models API currently doesn't have any way to allow this: it will only do the equivalent of SELECT *.

After discussing this on the django-developers list (http://groups-beta.google.com/group/django-developers/browse_thread/thread/488194de26e0ce18/), I've written a patch to add two functions to the models API at the module level: get_values() and get_values_iterator(). They act exactly like the get_list() and get_iterator() functions, but instead of returning a list (or iterator) of model API objects, they return a list (or iterator) of dictionaries. They also allow a new keyword parameter, "fields", which is a list (or tuple) of field names to return from the SELECT.

Thus, with the following patch, doing "SELECT DISTINCT year, month FROM blog ORDER BY year ASC, month ASC" would be written "results = blog.get_values(fields=['year','month'], order_by=['year','month'], distinct=True)".

Attachments (1)

get_values.patch (3.0 KB) - added by rmunn@… 10 years ago.
Patch to add get_values() and get_values_iterator()

Download all attachments as: .zip

Change History (3)

Changed 10 years ago by rmunn@…

Patch to add get_values() and get_values_iterator()

comment:1 Changed 10 years ago by adrian

  • Status changed from new to assigned

comment:2 Changed 10 years ago by adrian

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

(In [359]) Fixed #214 -- Added get_values() and get_values_iterator() module-level functions to DB API. Thanks, rmunn

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