Python django.core.exceptions.ImproperlyConfigured
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26103005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django.core.exceptions.ImproperlyConfigured
提问by Jon
I have recently moved to another machine and have had to check my project out from subversion again, but I am pretty sure this computer has Django 1.8 and the project is expecting 1.7.
我最近搬到了另一台机器上,不得不再次从 subversion 中检查我的项目,但我很确定这台计算机有 Django 1.8 并且该项目期待 1.7。
I have tried to sync my db with the code to create the necessary tables but I get the following error.
我试图将我的数据库与代码同步以创建必要的表,但出现以下错误。
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.
采纳答案by karthikr
Like the error mentions, you need to explicitlyspecify the fields, or exclude.
与错误提到的一样,您需要明确指定字段或排除。
Try this
尝试这个
class JobForm(models.ModelForm):
#fields
class Meta:
model = Job
fields = "__all__"
which would include all the fields
这将包括所有领域
Here is the relevant documentation (release notes 1.6)
这是相关文档(发行说明 1.6)
Previously, if you wanted a ModelForm to use all fields on the model, you could simply omit the Meta.fields attribute, and all fields would be used.
This can lead to security problems where fields are added to the model and, unintentionally, automatically become editable by end users. In some cases, particular with boolean fields, it is possible for this problem to be completely invisible. This is a form of Mass assignment vulnerability.
For this reason, this behavior is deprecated, and using the Meta.exclude option is strongly discouraged. Instead, all fields that are intended for inclusion in the form should be listed explicitly in the fields attribute.
If this security concern really does not apply in your case, there is a shortcut to explicitly indicate that all fields should be used - use the special value "
__all__" for the fields attribute
以前,如果您希望 ModelForm 使用模型上的所有字段,您可以简单地省略 Meta.fields 属性,并且将使用所有字段。
这可能会导致安全问题,其中字段被添加到模型中,并且无意中会自动成为最终用户可编辑的。在某些情况下,特别是对于布尔字段,这个问题可能完全不可见。这是一种大规模分配漏洞。
因此,不推荐使用此行为,强烈建议不要使用 Meta.exclude 选项。相反,所有打算包含在表单中的字段都应该在 fields 属性中明确列出。
如果此安全问题确实不适用于您的情况,则有一个快捷方式可以明确指示应使用所有字段 -
__all__对 fields 属性使用特殊值“ ”
回答by Jon
Dropping to Django 1.7 seemed to do the trick. There didn't seem to be an easy way to adapt the code to fit Django 1.8.
降到 Django 1.7 似乎可以解决问题。似乎没有一种简单的方法可以使代码适应 Django 1.8。
回答by david euler
You can set fields or exclude in the ModelForm in Django 1.7. It changes in 1.8, you should set fields or exclude in the Meta class within ModelForm.
您可以在 Django 1.7 的 ModelForm 中设置字段或排除。它在 1.8 中发生了变化,您应该在 ModelForm 的 Meta 类中设置字段或排除。
class JobForm(models.ModelForm):
#fields
class Meta:
model = Job
fields = "__all__"

