About Model Form Fields

It's not recommendable to use the exclude attribute for model form meta, because with big projects, you will likely forget to add new model fields that need to be excluded. Instead, you should have the boring list of fields.

DON'T:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        exclude = [...]

DO:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = [...]

Here is a utility function that lists all the names of editable fields, but excludes the excluded ones:

def get_modelform_fields(form_class):
    model_fields = set(
        field.name
        for field in form_class._meta.model._meta.get_fields()
        if not (field.auto_created) and getattr(field, "editable", True)
    )

    excluded_fields = (
        set(form_class._meta.exclude) if form_class._meta.exclude else set()
    )

    default_fields = set(form_class.base_fields.keys())

    form_fields = (model_fields - excluded_fields) | default_fields

    return sorted(form_fields)

Use this function in Django Shell and then replace the exclude attribute with the fields attribute in your code.

>>> from utils import get_modelform_fields
>>> get_modelform_fields(MyModelForm)
[...]

Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2 Python 3 django-crispy-forms