简体   繁体   中英

Django admin: Prefetch choices in list_editable

I have a model Domain and I set it's ForeignKey Language model in list_editable in ModelAdmin .

The problem is that it causes lot of SQL queries according to django_debug_toolbar . I thought that I could solve it using select_related but it did not help because it selects only actual values, not all choices.

@register(Domain)
class DomainAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'main_url', 'language', 'max_depth', 'number_of_urls']
    list_editable = ['name', 'main_url', 'language', 'max_depth']
    list_select_related = ['language']

    #def get_queryset(self, request):
    #    return super(DomainAdmin, self).get_queryset(request).prefetch_related('language')

It still perform SQL query for every Domain to fetch all Language objects.

在此处输入图片说明

How to make it fetch once for all Domain s?

Django-cachalot is especially efficient in the Django administration website since it's unfortunately badly optimised (use foreign keys in list_editable if you need to be convinced).

https://django-cachalot.readthedocs.io/en/latest/introduction.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM