U' __模块__'创建内联formset时出错

时间:2015-12-14 16:40:30

标签: django django-models django-forms

我是Django的新手。我正在尝试使用inlineformset_factory包含多个模型表单,但我收到以下错误:

Exception Type: KeyError at /dataentry/dataentry
Exception Value: u'__module__'

这是我的formset和模型表单:

ProductFormSet = inlineformset_factory(Product, Size , 
    ProductImage, 
    can_delete=False, fields=('image',) , extra=1)


class ProductModelForm(forms.ModelForm):
    class Meta:
        model = Product
        exclude = ['is_verified' , 'in_stock']

这是完整的追溯

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  108.                 response = middleware_method(request)
File "C:\Python27\lib\site-packages\django\middleware\common.py" in process_request
  74.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
  647.         resolve(path, urlconf)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  522.     return get_resolver(urlconf).resolve(path)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  366.             for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  402.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  396.             self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\importlib\__init__.py" in import_module
  37.     __import__(name)
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\Plumpin\urls.py" in <module>
  23.     url(r'^', include('products.urls')),
File "C:\Python27\lib\site-packages\django\conf\urls\__init__.py" in include
  33.         urlconf_module = import_module(urlconf_module)
File "C:\Python27\lib\importlib\__init__.py" in import_module
  37.     __import__(name)
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\urls.py" in <module>
  5. from .views import product_detail
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\views.py" in <module>
  25. from .forms import ProductModelForm , ProductFormSet
File "C:\Users\lenovo\Desktop\plump\Plumpin\src\products\forms.py" in <module>
  9.     can_delete=False, fields=('image',) , extra=1)
File "C:\Python27\lib\site-packages\django\forms\models.py" in inlineformset_factory
  1038.     FormSet = modelformset_factory(model, **kwargs)
File "C:\Python27\lib\site-packages\django\forms\models.py" in modelformset_factory
  838.                              labels=labels, help_texts=help_texts, error_messages=error_messages)
File "C:\Python27\lib\site-packages\django\forms\models.py" in modelform_factory
  545.     return type(form)(class_name, (form,), form_class_attrs)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in __new__
  82.         module = attrs.pop('__module__')

Exception Type: KeyError at /dataentry/dataentry
Exception Value: u'__module__'

为什么会发生错误,我该怎么做才能解决?

1 个答案:

答案 0 :(得分:2)

ProductFormSet = inlineformset_factory(Product, Size, ProductImage, ...)

inlineformset_factory的第三个参数应该是模型表单类。从名称ProductImage,听起来你已经通过了一个模型。也许您打算使用ProductModelForm代替。