开发者

How to limit columns returned by Django query?

开发者 https://www.devze.com 2022-12-23 03:41 出处:网络
That seems simple enough, but all Django Q开发者_运维技巧ueries seems to be \'SELECT *\' How do I build a query returning only a subset of fields ?In Django 1.1 onwards, you can use defer(\'col1\', \

That seems simple enough, but all Django Q开发者_运维技巧ueries seems to be 'SELECT *'

How do I build a query returning only a subset of fields ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消