Reckonsys
Reckonsys

Reputation: 361

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

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?

Upvotes: 1

Views: 710

Answers (2)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85572

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.

Upvotes: 0

shellbye
shellbye

Reputation: 4858

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

Upvotes: 1

Related Questions