我正在尝试将 Django Haystack 设置为基于一些漂亮的 url 进行搜索。这是我的 urlpatterns。
urlpatterns += patterns('',
url(r'^search/$', SearchView(),
name='search_all',
),
url(r'^search/(?P<category>\w+)/$', CategorySearchView(
form_class=SearchForm,
),
name='search_category',
),
)
我的自定义 SearchView 类如下所示:
class CategorySearchView(SearchView):
def __name__(self):
return "CategorySearchView"
def __call__(self, request, category):
self.category = category
return super(CategorySearchView, self).__call__(request)
def build_form(self, form_kwargs=None):
data = None
kwargs = {
'load_all': self.load_all,
}
if form_kwargs:
kwargs.update(form_kwargs)
if len(self.request.GET):
data = self.request.GET
kwargs['searchqueryset'] = SearchQuerySet().models(self.category)
return self.form_class(data, **kwargs)
如果我尝试访问 /search/Vendor/q=Microsoft,我在运行 Django 开发 Web 服务器时不断收到此错误
UserWarning: The model u'Vendor' is not registered for search.
warnings.warn('The model %r is not registered for search.' % model)
这在我的页面上
The model being added to the query must derive from Model.
如果我访问 /search/q=Microsoft,它可以正常工作。还有另一种方法可以做到这一点吗?
感谢您的任何指点-Jay