django.core.exceptions.ImproperlyConfigured

新手上路,请多包涵

我最近搬到了另一台机器,不得不再次从颠覆中检查我的项目,但我很确定这台计算机有 Django 1.8,并且该项目期待 1.7。

我试图将我的数据库与代码同步以创建必要的表,但出现以下错误。

 C:\Users\jont\Documents\ATP\Webapp>manage.py syncdb
C:\Python27\lib\site-packages\admin_tools\utils.py:9: RemovedInDjango19Warning:
django.utils.importlib will be removed in Django 1.9.
from django.utils.importlib import import_module

c:\users\jont\documents\django-trunk\django\contrib\contenttypes\models.py:148:
RemovedInDjango19Warning: Model class django.contrib.contenttypes.models.ContentType doesn't de
her isn't in an application in INSTALLED_APPS or else was imported before its application was
loaded. This will no longer be supported in Django 1.9.
class ContentType(models.Model):

C:\Python27\lib\site-packages\admin_tools\dashboard\modules.py:8: RemovedInDjango19Warning: The
django.forms.util module has been renamed. Use django.forms.utils instead.
from django.forms.util import flatatt

C:\Python27\lib\site-packages\django_tables2\tables.py:171: RemovedInDjango19Warning: SortedDict
is deprecated and will be removed in Django 1.9. attrs["base_columns"] =
SortedDict(parent_columns)

C:\Python27\lib\site-packages\django_tables2\tables.py:193: RemovedInDjango19Warning: SortedDict
is deprecated and will be removed in Django 1.9.
attrs["base_columns"].update(SortedDict(cols))

Traceback (most recent call last):
File "C:\Users\jont\Documents\ATP\Webapp\manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "c:\users\jont\documents\django-trunk\django\core\management__init__.py", line 336, in
execute_from_command_line
utility.execute()
File "c:\users\jont\documents\django-trunk\django\core\management__init__.py", line 310, in
execute
django.setup()
File "c:\users\jont\documents\django-trunk\django__init__.py", line 23, in setup
apps.populate(settings.INSTALLED_APPS)
File "c:\users\jont\documents\django-trunk\django\apps\registry.py", line 115, in populate
app_config.ready()
File "c:\users\jont\documents\django-trunk\django\contrib\admin\apps.py", line 22, in ready
self.module.autodiscover()
File "c:\users\jont\documents\django-trunk\django\contrib\admin__init__.py", line 24, in
autodiscover
autodiscover_modules('admin', register_to=site)
File "c:\users\jont\documents\django-trunk\django\utils\module_loading.py", line 73, in
autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Python27\lib\importlib__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\jont\Documents\ATP\Webapp\jobs\admin.py", line 4, in <module>
from jobs.views import registration
File "C:\Users\jont\Documents\ATP\Webapp\jobs\views.py", line 12, in <module>
from jobs.forms import ApplicantForm, JobForm, \
File "C:\Users\jont\Documents\ATP\Webapp\jobs\forms.py", line 8, in <module>
class JobForm(forms.ModelForm):
File "c:\users\jont\documents\django-trunk\django\forms\models.py", line 272, in __new__
"needs updating." % name
django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields'   attribute or the 'exclude' attribute is prohibited; form JobForm needs updating.

django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form JobForm needs updating.

原文由 Jon 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 788
2 个回答

像错误提到的那样,您需要 明确 指定字段或排除。

尝试这个

class JobForm(models.ModelForm):
    #fields

    class Meta:
        model = Job
        fields = "__all__"

这将包括所有领域

这是相关 文档(发行说明 1.6)

以前,如果您希望 ModelForm 使用模型上的所有字段,您可以简单地省略 Meta.fields 属性,并且将使用所有字段。

这可能会导致安全问题,其中字段被添加到模型中,并且无意中自动变为可由最终用户编辑。在某些情况下,特别是对于布尔字段,此问题可能完全不可见。这是一种批量分配漏洞。

因此,不推荐使用此行为,强烈建议不要使用 Meta.exclude 选项。相反,所有旨在包含在表单中的字段都应该在 fields 属性中明确列出。

如果此安全问题确实不适用于您的情况,则有一个快捷方式可以明确指示应使用所有字段 - 对字段属性使用特殊值“ __all__

原文由 karthikr 发布,翻译遵循 CC BY-SA 3.0 许可协议

在 Django 1.7 中,您可以在 ModelForm 中设置字段或排除。它在 1.8 中发生了变化,您应该在 ModelForm 的 Meta 类中设置字段或排除。

 class JobForm(models.ModelForm):
#fields

class Meta:
    model = Job
    fields = "__all__"

原文由 david euler 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题