[英]How to get n search objects from a SearchQuerySet without changing the type?
I am trying to get the to 10 objects like : 我正在尝试达到10个对象,例如:
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'>
. 我想要的输出是: <class 'haystack.query.SearchQuerySet'>
但我得到的是<type 'list'>
。
Can some one please help me out? 有人可以帮帮我吗?
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. 查看源代码 ,您会看到q_auth[:10]
返回结果列表。 A SearchQuerySet
is lazy and might not have all the results until you retrieve them with slicing, ie q_auth[:10]
. 一个SearchQuerySet
是惰性的,可能没有所有结果,除非您使用切片即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. 因为您的SearchQuerySet
实例q_auth
SearchQuerySet
将无法用于获取更多结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.