Improve Performance

Learn to improve the performances of our Django admin site.

Retrieving foreign key

If your model has a foreign key (like the Author for our Question model or Question for our Choice model), set list_select_related to instruct Django to use select_related() in retrieving the list of objects on the admin change list page. This can save you a bunch of database queries. You can do the following for your QuestionAdmin.

list_select_related = ('refAuthor',)

And for your ChoiceAdmin you can do something similar:

list_select_related = ('question','question__refAuthor',)

Autocomplete fields

If you have a foreign key in your model, which could have thousands of results, you can set an autocomplete_fields whereby in the creation/edition form of your model, you will have a search box (to find authors). It is more efficient than selecting an Author with a select-box interface (default appearance in Django admin for foreign keys). But for it to work properly, you will need to define a search_fields in your foreign key model admin to indicate which fields you want to search.

TAKE a look at your Question/Author models.

In your AuthorAdmin model, you will add a search_field on the name:

class AuthorAdmin(admin.ModelAdmin):
	empty_value_display = 'Unknown'
	fieldsets = [
		("Author information", {'fields': ['name']}),
	]
	list_display = ('name','createdDate','updatedDate',)
	search_fields = ('name',)

And then on the QuestionAdmin, you will add an autocomplete_fields on the refAuthor foreign key

class QuestionAdmin(admin.ModelAdmin):
....
autocomplete_fields = ['refAuthor']
....

The interface will look like this:

Get hands-on with 1200+ tech skills courses.