Opened 14 years ago

Closed 14 years ago

Last modified 13 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 Holovaty
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: no UI/UX: no


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 (, 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@… 14 years ago.
Patch to add get_values() and get_values_iterator()

Download all attachments as: .zip

Change History (3)

Changed 14 years ago by rmunn@…

Attachment: get_values.patch added

Patch to add get_values() and get_values_iterator()

comment:1 Changed 14 years ago by Adrian Holovaty

Status: newassigned

comment:2 Changed 14 years ago by Adrian Holovaty

Resolution: fixed
Status: assignedclosed

(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