我試圖值的不同列表加載到ModelChoiceField
。加載不同的值到ModelChoiceField的Django表單
class myform(ModelForm):
content = forms.FileField()
def distinct_mans():
manufacturers = UniPart.objects.all().values('manufacturer').distinct()
return manufacturers
manufacturer = forms.ModelChoiceField(queryset=distinct_mans)
要說明,製造商是UniPart表中的一個字段。我想要獲得所有不同的製造商並將其放入下拉菜單中。
但是我得到這個錯誤:
'function' object has no attribute 'all'
回溯:
Template error:
In template /Users/nb/Desktop/modelstore/mymodels/templates/mymodels/add_model.html, error at line 24
'function' object has no attribute 'all'
14 : {% csrf_token %}
15 : <br>
16 : <div style="float: left; width: 200px">
17 : <h6>Model Name</h6>
18 : <br>
19 : {{ formtoaddmodel.modelname }}
20 : <br>
21 : <br>
22 : <h6>Manufacturer</h6>
23 : <br>
24 : {{ formtoaddmodel.manufacturer }}
Traceback:
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nb/Desktop/modelstore/mymodels/views.py" in add_model
652. }, context_instance=RequestContext(request,{}))
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
176. return t.render(context_instance)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
62. return self.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
62. return self.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
62. return self.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
62. return self.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/base.py" in render
823. bit = self.render_node(node, context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render_node
74. return node.render(context)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/template/debug.py" in render
87. output = force_unicode(output)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/utils/encoding.py" in force_unicode
71. s = unicode(s)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/forms/forms.py" in __unicode__
411. return self.as_widget()
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/forms/forms.py" in as_widget
458. return widget.render(name, self.value(), attrs=attrs)
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/forms/widgets.py" in render
547. options = self.render_options(choices, [value])
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/forms/widgets.py" in render_options
570. for option_value, option_label in chain(self.choices, choices):
File "/Users/nb/Desktop/myenv2/lib/python2.7/site-packages/django/forms/models.py" in __iter__
896. for obj in self.queryset.all():
Exception Type: AttributeError at /mymodels/add
Exception Value: 'function' object has no attribute 'all'
謝謝你,我以前就是這樣試過的,但是它沒有正常工作(太多的值來解壓錯誤)。不知道我做了什麼不同,但它一定是小事。 – user1328021
建議的代碼返回重複值 – Dennis