简体   繁体   中英

class Meta VS form fields in Django

I'm learning Django forms from this example. When I run my server I get django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited (solved here ). Using class Meta everything works. Explain pliese could I use these both aproaches with Django==2.2.6 and pros and cons of them.

After Django 1.8, it was required that forms explicitly indicate which fields they wanted to display in class Meta, so you definitely need to do it in 2.2.6.

fields is an inclusive way to listing fields. If you explicitly want to list which fields are included in the form, you put them here. This means that if you ever change your model, the new fields in the model will not automatically be added to the form unless you set fields to __all__

exclude is unsurprisingly the opposite of that. It means that the form includes all fields except those explicitly listed in exclude. If you change your model, new fields will be added to the form unless you change what is in exclude .

The differences are not really that large in the grand scheme of things. It just depends on whether you want to explicitly specify what is included or what is excluded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM