Why is a Django model taking so long to load in admin?

Try to add all foreign keys in raw_id_fields in admin

class PostReplyAdmin(ModelAdmin):
     raw_id_fields = ['user', 'post', 'quoted_post']

This will decrease page's load time in change view. The problem is that django loads ForeignModel.objects.all() for each foreign key's dropdowns.


Another way is to add foreign keys in autocomplete_fields (docs) in admin

class PostReplyAdmin(ModelAdmin):
    autocomplete_fields = ['user', 'post', 'quoted_post'] 

As pointed by @Andrey Nelubin the problem for me was indeed in the page loading all related models for each foreign key's dropdown. However, with autocomplete_fields selects are turned into autocomplete inputs (see figure below), which load options asynchronously.

enter image description here

Tags:

Django