[Django]-Django Admin Million Data – Admin page takes too long to open

2πŸ‘

βœ…

This is likely because of an N+1 problem where it first will retrieve all the PollVOte objects, and then it has to make N queries to load the titles.

We can use the list_select_related [Django-doc] option to add these to the .select_related(…) [Django-doc] items that will then be fetched in the same query:

@admin.register(PollVote)
class PollVoteAdmin(admin.ModelAdmin):
    list_display = ['title', 'referer', 'vote', 'ip', 'created_date']
    ordering = ('-created_date',)
    search_fields = ['ip']
    raw_id_fields = ['title']
    list_select_related = ['title']

It might be better however to rename the ForeignKey to the Poll model to Poll, not title, since title would hint towards a CharField.

Leave a comment