Reinout van Rees - Improve your django admin: big gains with little effort

published May 13, 2016, last modified May 17, 2016

Reinout van Rees talks about improving your django admin: big gains with little effort, at PyGrunn.

Reinout van Rees talks about improving your django admin: big gains with little effort, at PyGrunn.

See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.

The admin interface is Django's wrapper around the database. It has lots of possibilities that usually just take a few lines of code for a big improvement.

Three parts: you have the main admin page, a list of objects, and an object edit page.

What I tell you is all in the documentation of the Django admin.

Main admin page:

  • In admin.py register all your models so they end up here.
  • Give a verbose_name and verbose_name_plural in models.py in the Meta.

List of objects. I usually change these five items:

  • Add a __unicode__ method on your model. Otherwise you see Token object as name. Don't make it too long, needing to access five database models for your name.
  • Sort order. In your model Meta add something like: ordering = ('name',). Same in your admin model if you need a different order there.
  • Columns. In the admin model specify fields to show: list_display = ['name', 'url']. These can also be method names defined in your model.
  • Search. Please add a search box in admin: search_fields = ['name', 'url']. For this half minute of work I got lots of praise from colleagues.
  • Filters. In the admin add a filter: list_filter = ['portals', 'organisations'].

All these are just one line. If you take over a project, this is a nice way of improving it and at the same time getting to know the database.

Object edit page:

  • Order of fields. Default is just the order in which fields are specified in your model. So in your model put them in the right order. In admin you can add fieldsets to group fields, though this is not really one line.
  • Specify verbose_name, help_text on the model: looks much nicer in the admin interface.
  • readonly_fields in admin.
  • Easier selection. You may have a dropdown of four hundred items, hard to select. But you can use a nicer widget: filter_horizantal with list of fields in admin.
  • Faster selection. Tens of thousands of related model objects in a dropdown can make it very slow to display. Solution: row_id_fields in admin.
  • Inline objects. Edit an object and on this edit page add a different model inline. In the admin define one model as TabularInline and add inlines on the parent admin model.

Others:

  • Queryset filtering, to see only items that you have for example edit permission for.
  • Are you missing the option to add a model? Then you are missing an admin model.
  • You can define actions.

So: you can do various really easy things to make the django admin much nicer.

If you want to give users access to the admin, you can give him staff status and give permissions to view, add, or edit.

Documentation: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/

Twitter: @reinoutvanrees