Opened 19 years ago

Closed 19 years ago

Last modified 18 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

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

Download all attachments as: .zip

Change History (3)

by rmunn@…, 19 years ago

Attachment: get_values.patch added

Patch to add get_values() and get_values_iterator()

comment:1 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:2 by Adrian Holovaty, 19 years ago

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