我有一个用于高级搜索的表格。其中一个字段island_group
是一个 ModelChoiceField,我为其返回 avalues_list
作为查询集(以便只获取每个不同的值一次)。
表格.py:
class SearchForm(forms.Form):
...
island_group = forms.ModelChoiceField(
required=False,
queryset=Locality.objects.values_list('islandgroup', flat=True).distinct('islandgroup')
...
并且因为这会引发错误,invalid choice
因为我使用的是值而不是对象,所以我覆盖了错误:
def clean(self):
cleaned_data = super(SearchForm, self).clean()
if self._errors["island_group"]:
del self._errors["island_group"]
return cleaned_data
但是,我不断收到KeyError at /collections/ 'island_group'
错误。事实上,由于这些数据是从只读数据库中提取的,我只是希望能够以最小的麻烦将其传递到视图中。
任何帮助将非常感激。另外,这是回溯:
Traceback:
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/sam/django-projects/datazone/bin/datazone/cdrs/views.py" in cdrs_index
59. return render(request, 'cdrs_index.html', context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
44. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
188. return t.render(context_instance)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render
123. return self._render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
57. return self.nodelist.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
127. return compiled_parent._render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
57. return self.nodelist.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
64. result = block.nodelist.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render
744. bits.append(self.render_node(node, context))
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render_node
757. return node.render(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
227. nodelist.append(node.render(context))
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in render
792. output = self.filter_expression.resolve(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in resolve
510. obj = self.var.resolve(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in resolve
653. value = self._resolve_lookup(context)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
683. current = getattr(current, bit)
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/forms/forms.py" in _errors
415. return self.form.errors.get(self.name, self.form.error_class())
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/forms/forms.py" in _get_errors
112. self.full_clean()
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
268. self._clean_form()
File "/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
296. self.cleaned_data = self.clean()
File "/home/sam/django-projects/datazone/bin/datazone/cdrs/forms.py" in clean
113. if self._errors["island_group"]:
Exception Type: KeyError at /collections/
Exception Value: 'island_group'