Django Haystack Search by Django Model PK

北城余情 提交于 2019-12-13 04:33:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!