Django Admin App Views

Following my previous post about configuring Django models, I’m now ready to work on the view. Remember, this is simply within the “Admin” app which ships with Django, and below is a non-exhaustive set of options:

ModelAdmin Derived Class Options

For the list screen:

  • list_display = ('field1', 'field2')
  • list_display_links = ['field2']
  • list_editable = ['field2']
  • list_filter = ('field1', 'field2')
  • list_per_page = 20
  • search_fields = ['field1', 'foreign_key__fieldname']

For the edit screen, it’s possible to control the form layout and displayed fields:

fieldsets = (("Title"|None, options), )
options = {
    fields : (('one', 'line', 'together'), 'field5', 'field6'),
    classes : ['collapse', 'wide']
}

or use the following:

fields = ('field1', 'field2')
exclude = ('field3', 'field4')

And some other edit screen options:

  • filter_horizontal|filter_vertical = ('multi_select_field')
  • radio_fields = ('fk_field', 'choice_field')
  • save_on_top = True

For further details, see the Admin App documentation. Also useful are the Custom Actions documentation, the DB Queries documentation, and the QuerySet documentation.

This entry was posted in databases, python. Bookmark the permalink.

Comments are closed.