如何限制 Django 查询返回的列? [英] How to limit columns returned by Django query?

查看:25
本文介绍了如何限制 Django 查询返回的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很简单,但所有 Django 查询似乎都是 'SELECT *'

That seems simple enough, but all Django Queries seems to be 'SELECT *'

如何构建仅返回字段子集的查询?

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

推荐答案

在 Django 1.1 以后,您可以使用 defer('col1', 'col2') 从查询中排除列,或者only('col1', 'col2') 只获取一组特定的列.请参阅文档.

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 做了一些稍微不同的事情 - 它只获取您指定的列,但它返回一个字典列表而不是一组模型实例.

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.

这篇关于如何限制 Django 查询返回的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆