1

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?

Subhajit
  • 361
  • 1
  • 4
  • 18

2 Answers2

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])
shellbye
  • 4,620
  • 4
  • 32
  • 44
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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • It's converting the type. I want the type to be `` but it gives me a list – Subhajit Dec 13 '15 at 05:32
  • `q_auth` is of this type already. Slicing returns a list. That is how it is implemented. Actually, it makes a lot of sense. Please try the code in my answer. Use a different name for your slicing result and `q_auth` keeps its type. – Mike Müller Dec 13 '15 at 05:41
  • I tried the code in your answer, but it changed the type. So I changed my code to use a list rather than the search object. – Subhajit Dec 13 '15 at 06:33
  • Good to hear. If this answer contributed to the solution, consider accepting. – Mike Müller Dec 15 '15 at 07:03
  • Sorry but this did not help much as it was changing the type , I had to rewrite my code for a list – Subhajit Dec 15 '15 at 07:05