That seems simple enough, but all Django Queries seems to be 'SELECT *'
How do I build a query returning only a subset of fields ?
The Python docs describe reduce as: Applying a function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
Django values_list() is an optimization to grab specific data from the database instead of building and loading the entire model instance.
all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result.
In Django 1.1 onwards, you can use defer('col1', 'col2')
to exclude columns from the query, or only('col1', 'col2')
to only get a specific set of columns. See the documentation.
values
does something slightly different - it only gets the columns you specify, but it returns a list of dictionaries rather than a set of model instances.
Append a .values("column1", "column2", ...)
to your query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With