Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.

django 报错

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

一搜网上的回答大都是说加上

fields = '__all__'

看下django.forms.models 中判断报错的源码如下

            # If a model is defined, extract form fields from it.
            if opts.fields is None and opts.exclude is None:
                raise ImproperlyConfigured(
                    "Creating a ModelForm without either the 'fields' attribute "
                    "or the 'exclude' attribute is prohibited; form %s "
                    "needs updating." % name
                )

意思就是定义的model没字段 但我有的啊

class ResumeForm(forms.ModelForm):
    class Meta:
        model = Resume
        field = ('title', 'body',)

再仔细一看 fields 写成 field ...

原文地址:https://www.cnblogs.com/hehecat/p/8638650.html