Customizing model add or change form in django admin

Dec 29, 2016

The get_form method in django admin returns a ModelForm class for use in the admin add and change views. This can be overridden to customize the add and change forms of a model in the admin area.

class MyModelAdmin(admin.ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        if obj is None:
            kwargs['fields'] = ['one_field', 'another_field']
        else:
            kwargs['fields'] = ['one_field', 'another_field', 'some_more_field']

        return super(MyModelAdmin, self).get_form(request, obj, **kwargs)