我看过这样的查询:
user = User.objects.all() or User.objects.filter(username = username)
Run Code Online (Sandbox Code Playgroud)
即使我们不需要所有列,也会获取表的所有列.我们有更好的方法来编写数据库查询吗?如果是,我们为什么不经常看到这个代码呢?
max*_*max 26
使用django 1.8:使用values_list
Entry.objects.values_list('id', 'headline')
Run Code Online (Sandbox Code Playgroud)
https://docs.djangoproject.com/en/1.8/ref/models/querysets/
如果您只需要将值作为字典使用objects.values('').它也更快.
见文档:http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields
| 归档时间: |
|
| 查看次数: |
46816 次 |
| 最近记录: |