, Johann Schmitz

This article is part of the Customizing the Django admin application series:

  1. Django Admin customizing
  2. Django Admin customizing - part 2
  3. Django Admin customizing - part 3

After customizing the list view (part 1 and part 2) it's time to look at the create / update view. By default, Django renders a form based on the ordering and the type of the model fields:

Basic update view

The ModelAdmin class provides a lot of fields which allows to customize the appearance of the view without even touching the templates. Here are a few examples (check out the documention to see what other options are possible):

  • fieldsets: Allows grouping of model fields
  • readonly_fields: Disable the rendering of an input field for some model fields

Let's extend our existing CountryModelAdmin class by grouping the fields:

class CountryModelAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {
            'fields': [ 'name', 'capital' ]
        ('Facts', {
            'fields': ['area', 'population', 'currency', 'currency_name', 'tld'],
        ('Misc', {
            'fields': [ 'iso_code', 'iso3_code', 'iso_numeric_code' ],
            'classes': ['collapse']

As you can see, the fieldsets field holds a list of tuples (title, options) of our field sets. The options can hold a wide range of settings (you probably want to set at least fields):

Djano update view with fieldsets

Since we don't want the users mess with the iso code values, we disable editing in the admin application:

readonly_fields = ['iso_code', 'iso3_code', 'iso_numeric_code']

Model fields in readonly_fields are rendered as plain text:

Django admin fieldset with readonly_fields