Django RawQuerySet无法按预期工作 [英] Django RawQuerySet not working as expected

查看:52
本文介绍了Django RawQuerySet无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照Django文档的要求运行了原始查询:

I ran a raw query just as the Django docs point:

Model.objects.raw('SELECT * FROM model')

我得到了这个结果,为什么它只显示对象?

and I got this result, Why it only shows the object?

<RawQuerySet: 'SELECT * FROM model'>

推荐答案

由于queryset的一项功能(非常强大)是,除非有必要,否则它不会访问数据库.您可以对其进行切片以获得结果.

Because one (very powerful) feature of a queryset is, that it does not hit the database until necessary. You might slice it to get the results.

Model.objects.raw('SELECT * FROM model')[:]

阅读的第一段queryset文档,以了解有关执行基础查询的其他方式的更多信息.

Read the first paragraph of the queryset docs to learn more about other ways to execute the underlying query.

这篇关于Django RawQuerySet无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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