[Solved]-Django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited

27πŸ‘

When you are creating a form using model, you need to specify which fields you want to be included in the form.

For example you have a model with the named Article and you want to create a form for the model article.

pub_date, headline, content, reporter these are the fields in the model.
If you choose to include all the fields you do like this.

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = '__all__'

If you want to specify which fields you want to include

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ['pub_date', 'headline', 'content']

If you want to exclude a certain fields and use the remaining then use as follows

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        exclude = ['headline']

Coming to your error, it says you can use ModelForm with out using one of the two options that is using fields or exclude.

πŸ‘€kartheek

6πŸ‘

class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic
        fields = '__all__' # or whatever fields you want ('field_a', )

3πŸ‘

I had the exact same error and turns out I had written β€˜field’ instead of β€˜fields
β€˜

2πŸ‘

class BlogForm(forms.ModelForm):
    class Meta:
        model=Blog
        fields='__all__'

check name fields, you type incorrectly, or capital F is used.

πŸ‘€JANKI

0πŸ‘

from django import forms
from .models import Dforms

class Forms(forms.ModelForm):
    class Meta:
        model = Dforms
        fields = "__all__"

0πŸ‘

I had the same problem, my mistake was to miss the name of a class in my app’s forms.py file

-1πŸ‘

i had aslo same problem after i checked my code again then i find i had written field istead of fields so first check name fields, you may have type incorrectly.

πŸ‘€Ratnesh Yadav

Leave a comment