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

为君一笑 提交于 2019-12-08 08:48:47

问题


I am trying to get the to 10 objects like :

q_auth = SearchQuerySet().filter(content=validate_query)
q_auth = q_auth[:10]
print type(q_auth)

The output I want is: <class 'haystack.query.SearchQuerySet'> but I am getting is <type 'list'>.

Can some one please help me out?


回答1:


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])



回答2:


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.



来源:https://stackoverflow.com/questions/34240104/how-to-get-n-search-objects-from-a-searchqueryset-without-changing-the-type

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