Translatable Manytomany fields in admin generate many queries

怎甘沉沦 提交于 2019-12-04 09:55:01

In the hope of being useful for some others, here is how I solved the problem, reducing the queries from 2k to 30 in the admin:

class MyModelAdminForm(TranslatableModelForm):
    class Meta:
        model = MyModel
        exclude = ()

    def __init__(self, *args, **kwargs):
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
        self.fields['services'].queryset = Service.objects.prefetch_related('translations').all()

class MyModelAdmin(TranslatableAdmin):

    form = MyModelAdminForm

So, override the form, and once inside, override the queryset with prefetch.

wobbily_col

Looks like you are using a double underscore for the many-to-many table, when it should be a single underscore. Also try adding in the master table

try :

return super(DoctorAdmin, self).get_queryset(request).prefetch_related( 
    'services__service_translation__translations',
    'services__service_translation__master'
)

Showing the models.py file would help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!