1

値の個別のリストをにロードしようとしています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'
4

2 に答える 2

1

まあ、クエリセット引数は関数ではなくクエリセットを期待しています。

しかし、この機能は不要です - あなたはこれを行うことができます:

manufacturer = forms.ModelChoiceField(queryset=UniPart.objects.all().values('manufacturer').distinct())

これはまさにあなたが望むことをするはずです。

于 2012-11-14T21:00:16.963 に答える
0

querysetどうやらcallableを取らないようです。幸いなことに、それは必要ありません。クエリセットを一度構築し、queryset引数として直接渡すだけModelChoiceFieldです。

于 2012-11-14T20:59:52.740 に答える