我正在尝试将不同的值列表加载到ModelChoiceField
.
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'