Filter haystack result with SearchQuerySet

≡放荡痞女 提交于 2019-12-24 09:53:11

问题


I'm using haystack with whoosh, trying to restrict search results to entries created by the currently logged in user only. The category model, for which I created an index, has a foreign key:

user = models.ForeignKey(User, editable=False)

And in my custom search view I want to filter like this:

searchqueryset = SearchQuerySet().filter(user=request.user.id)

form = SearchForm(request.GET, searchqueryset=searchqueryset, load_all=True)

    if form.is_valid():
        query = form.cleaned_data['q']
        results = form.search()

In the database there is one entry for table category:

id  name    user_id 
1   test10  1   

And the current user id is indeed 1. But I get No results.

When I do this:

searchqueryset = None

I get the "test10" category entry. So does anyone know why the user id filter isn't working as expected?


回答1:


Are you sure that the "user" in your Haystack index holds the integer value of user id? Perhaps it has some other user data(such as username) there, so the comparison simply does not work?

By default the Django user object returns the user name, not the user id.



来源:https://stackoverflow.com/questions/7351038/filter-haystack-result-with-searchqueryset

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