Why do we need fields value in Meta class in Django forms?

1k views Asked by At

If I have 3 fields in my Form class and in fields I set only one field, on my html page still will be 3 fields.

Example:

class CategoryForm(forms.ModelForm):
    name = forms.CharField(max_length=128,
                           help_text='Please enter the category name.')
    views = forms.IntegerField(initial=0)
    likes = forms.IntegerField(initial=0)
    slug = forms.CharField(widget=forms.HiddenInput(), initial=0)

    class Meta:
        model = Category
        fields = ('name',)

So fields variable doesn't hide anything?

1

There are 1 answers

0
Sativa On BEST ANSWER

"The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute."

If you want to hide fields, use

exclude = ('views','likes','slug')

I highly recommend you to take a look at the docs:

https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#selecting-the-fields-to-use