English 中文(简体)
Filter Django Haystack results like QuerySet?
原标题:

Is it possible to combine a Django Haystack search with "built-in" QuerySet filter operations, specifically filtering with Q() instances and lookup types not supported by SearchQuerySet? In either order:

haystack-searched -> queryset-filtered

or

queryset-filtered -> haystack-searched

Browsing the Django Haystack documentation didn t give any directions how to do this.

最佳回答

You could filter your queryset based on the results of a Haystack search, using the objects PKs:

def view(request):
  if request.GET.get( q ):
    from haystack import ModelSearchForm
    form = ModelSearchForm(request.GET, searchqueryset=None, load_all=True)
    searchqueryset = form.search()
    results = [ r.pk for r in searchqueryset ]

    docs = Document.objects.filter(pk__in=results)
    # do something with your plain old regular queryset

    return render_to_response( results.html , { documents : docs});

Not sure how this scales, but for small resultsets (a few hundred, in my case), this works fine.

问题回答

From the docs:

SearchQuerySet.load_all(self)

Efficiently populates the objects in the search results. Without using this method, DB lookups are done on a per-object basis, resulting in many individual trips to the database. If load_all is used, the SearchQuerySet will group similar objects into a single query, resulting in only as many queries as there are different object types returned.

http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#load-all

Therefore, after you have a filtered SQS, you can do a load_all() on it and just access the database objects via SearchResult.object. E.g.

sqs = SearchQuerySet()
# filter as needed, then load_all
sqs = sqs.load_all()

for result in sqs:
    my_obj = result.object
    # my_obj is a your model object

If you want to keep up with the pertinence, you have to access the object from the database through "object" :

example in your template:

{% for result in results %}
    {{ result.object.title }}
    {{ result.objects.author }}
{% endfor %}

But this is really bad since haystack will make an extra request like "SELECT * FROM blah WHERE id = 42" on each results.

Seems you re trying to get those object from your database because you didn t put some extra fields in your index ins t it ? If you add the title AND the author in your SearchIndex, then you can just use your results:

{% for result in results %}
    {{ result.title }}
    {{ result.author }}
{% endfor %}

and avoid some extra queries.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签