问题
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