所以我尝试使用自定义搜索表单,以便我可以分别搜索多个模型字段。 (比如,按资产#,按部门等搜索设备)我现在有以下代码:
SEARCH_CHOCIES = (('Asset #','Asset #'), ('Calibrated by','Calibrated by'), ('Department','Department'),
('Description','Description'), ('Expiring soon','Expiring soon'), ('Flagged','Flagged'),
('Manufacturer','Manufacturer'), ('Model','Model'), ('Serial #','Serial #'), ('Test','Test'))
class MultipleSearchForm(SearchForm):
search_type = forms.ChoiceField(required = False, choices = SEARCH_CHOCIES)
search_content = forms.CharField(required = False)
def search(self):
sqs = super(MultipleSearchForm, self).search()
if not self.is_valid():
return self.no_query_found()
choice = self.cleaned_data['search_type']
content = self.cleaned_data['search_content']
if choice:
if content:
if choice == 'Asset #':
sqs = sqs.filter(self.cleaned_data['asset_number'] == content)
return sqs
urls.py:
url(r'^search/', search_view_factory(view_class = SearchView, form_class = MultipleSearchForm), name='haystack_search'),
现在它只在您选择按资产#搜索时处理搜索。我有以下问题:
/ calbase / search /
中的KeyError' asset_number'
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/calbase/search/?q=&search_type=Asset+%23&search_content=S2
Django Version: 1.10
Python Version: 3.5.2
Installed Applications:
['calbase.apps.CalbaseConfig',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'haystack',
'whoosh',
'carton',
'adminplus',
'django.contrib.admin.apps.SimpleAdminConfig',
'nested_inline']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\haystack\views.py" in search_view
157. return view_class(*args, **kwargs)(request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\haystack\views.py" in __call__
49. self.results = self.get_results()
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\haystack\views.py" in get_results
89. return self.form.search()
File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\search_indexes.py" in search
38. sqs = sqs.filter(self.cleaned_data['asset_number'] == content)
Exception Type: KeyError at /calbase/search/
Exception Value: 'asset_number'
这里我只想比较" asset_number"查询结果字段与搜索内容一起查找我们想要搜索的内容。这里有什么问题?
现在在搜索视图中,除了我创建的搜索类型和搜索内容之外,还有旧的"搜索"领域。我怎么杀了它?
对于某些搜索类型,例如部门和测试,我希望搜索内容字段也是一个选择字段,以便用户可以从下拉菜单中进行选择。对于所有其他搜索类型,我希望搜索内容只是一个字段。这可行吗?
非常感谢