简体   繁体   English

如何在不更改类型的情况下从SearchQuerySet获取n个搜索对象?

[英]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.

相关问题 如何在不更改顺序的情况下将SearchQuerySet转换为QuerySet? - How to convert SearchQuerySet into QuerySet without changing order? 有没有一种方法可以将SearchQuerySet转换为objects.filter返回的类型? -Django - Is there a way to convert SearchQuerySet to the type that objects.filter returns? -Django 如何在不更改类型的情况下将充满日期时间对象的元组列表转换为日期时间对象列表? - How to convert a list of tuples full of datetime objects into a list of datetime objects without changing the type? 如何在没有 \n 的情况下从 txt 中获取特定行(Python) - How to get a specific line from a txt without \n (Python) 如何在不更改对象本身的情况下遍历对象列表? - How to loop through a list of objects without changing the objects themselves? 如何在不更改类型的情况下连接日期时间 arrays? - How to concatenate datetime arrays without changing the type? 如何在不读取 Django 中的所有对象的情况下从 DB 中获取一些最后的对象? - how can I get some last objects from DB without reading all of the objects in Django? 干草堆,SearchQuerySet()。filter不返回任何内容。 (Django +弹性搜索) - Haystack, SearchQuerySet().filter is not returning anything. ( django + elastic search) Django Haystack和Whoosh搜索正常,但SearchQuerySet返回0个结果 - Django Haystack & Whoosh Search Working, But SearchQuerySet Return 0 Results 在 Django 的一个地方从多个模型中获取 N 个最近的对象 - Get N recent objects from multiple models at one place in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM