如何仅在Django中提取表的特定列? [英] How to fetch only specific columns of a table in django?

查看:145
本文介绍了如何仅在Django中提取表的特定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到类似这样的查询:

I have seen that queries like :

user = User.objects.all() or User.objects.filter(username = username)

将获取表的所有列,即使我们不需要所有列。我们是否有更好的编写数据库查询的方式?如果是,为什么我们不经常看到该代码?

will fetch all the columns of the table even if we do not need all the columns. Do we have a better way of writing a database query? and if yes why do we not see that code most often?

推荐答案

QuerySet.only() QuerySet.defer() 可用于优化ORM将拉出的字段,将其他字段推迟到访问模型上的适当属性为止。

QuerySet.only() and QuerySet.defer() can be used to refine which fields the ORM will pull, deferring the others until the appropriate attributes on the models are accessed.

这篇关于如何仅在Django中提取表的特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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