问题
Is there an easy hook for looking up Haystack results by Django model PK? Something like (this doesn't work)
SearchQuerySet().filter(pk=12)
The alternative is that I would add an explicit field to the SearchIndex for the model pk, but that seems wasteful, since the queryset results from Haystack contain the underlying django model pk
回答1:
Following worked for me:
SearchQuerySet().models(Product).filter(django_id='10229')
回答2:
In Haystack you might have a lot of different Apps and Models, thus you must explicitly also name those for the haystack query, because internally Haystack will create ids like app.model.pk ... E.g. you might have an App "Catalog" inside which you have a Model "Product" and want to filter for pk=12:
SearchQuerySet().filter(pk='catalog.product.12')
will do the trick.
来源:https://stackoverflow.com/questions/18276957/django-haystack-search-by-django-model-pk