How to get n search objects from a SearchQuerySet without changing the type?

匆匆过客 提交于 2019-12-07 18:10:31

I tried something similar like your code but got the output like this:

<class 'django.db.models.query.QuerySet'>

Based on what you've got, I think you can try something like:

print type(q_auth[0])

Looking at the source, you will see that q_auth[:10] returns a list of results. A SearchQuerySet is lazy and might not have all the results until you retrieve them with slicing, i.e. q_auth[:10].

Just do:

first_results = q_auth[:10]   

and access a result with:

first_results[0]

I recommend not to do this:

q_auth = q_auth[:10]

because your instance q_auth of SearchQuerySet would not be available for retrieving more results later.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!