Django Inline for ManyToMany生成重复查询

时间:2016-11-17 21:56:43

标签: django duplicates django-queryset inline-formset

我的django管理员遇到了一些重大的表现问题。基于我有多少内联的重复查询。

models.py

class Setting(models.Model):
    name = models.CharField(max_length=50, unique=True)

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name


class DisplayedGroup(models.Model):
    name = models.CharField(max_length=30, unique=True)
    position = models.PositiveSmallIntegerField(default=100)

    class Meta:
        ordering = ('priority',)

    def __str__(self):
        return self.name


class Machine(models.Model):
    name = models.CharField(max_length=20, unique=True)
    settings = models.ManyToManyField(
        Setting, through='Arrangement', blank=True
    )

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name


class Arrangement(models.Model):
    machine = models.ForeignKey(Machine, on_delete=models.CASCADE)
    setting = models.ForeignKey(Setting, on_delete=models.CASCADE)
    displayed_group = models.ForeignKey(
        DisplayedGroup, on_delete=models.PROTECT,
        default=1)
    priority = models.PositiveSmallIntegerField(
        default=100,
        help_text='Smallest number will be displayed first'
    )

    class Meta:
        ordering = ('priority',)
        unique_together = (("machine", "setting"),)

admin.py

class ArrangementInline(admin.TabularInline):
    model = Arrangement
    extra = 1


class MachineAdmin(admin.ModelAdmin):
    inlines = (ArrangementInline,)

如果我在内联表单上添加了3个设置,另外还有1个设置,我有大约10个重复查询

SELECT "corps_setting"."id", "corps_setting"."name", "corps_setting"."user_id", "corps_setting"."tagged", "corps_setting"."created", "corps_setting"."modified" FROM "corps_setting" ORDER BY "corps_setting"."name" ASC
- Duplicated 5 times

SELECT "corps_displayedgroup"."id", "corps_displayedgroup"."name", "corps_displayedgroup"."color", "corps_displayedgroup"."priority", "corps_displayedgroup"."created", "corps_displayedgroup"."modified" FROM "corps_displayedgroup" ORDER BY "corps_displayedgroup"."priority" ASC
- Duplicated 5 times.

有人可以告诉我,我在这里做错了什么吗?我花了3天的时间试图自己解决问题而没有运气。

当我有一台机器的大约50个设置内联时问题会变得更糟,我会有~100个查询。

Here is the screenshot

3 个答案:

答案 0 :(得分:5)

我已经根据@ makaveli的答案组装了一个通用的解决方案,评论中似乎没有提到问题:

class CachingModelChoicesFormSet(forms.BaseInlineFormSet):
    """
    Used to avoid duplicate DB queries by caching choices and passing them all the forms.
    To be used in conjunction with `CachingModelChoicesForm`.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        sample_form = self._construct_form(0)
        self.cached_choices = {}
        try:
            model_choice_fields = sample_form.model_choice_fields
        except AttributeError:
            pass
        else:
            for field_name in model_choice_fields:
                if field_name in sample_form.fields and not isinstance(
                    sample_form.fields[field_name].widget, forms.HiddenInput):
                    self.cached_choices[field_name] = [c for c in sample_form.fields[field_name].choices]

    def get_form_kwargs(self, index):
        kwargs = super().get_form_kwargs(index)
        kwargs['cached_choices'] = self.cached_choices
        return kwargs


class CachingModelChoicesForm(forms.ModelForm):
    """
    Gets cached choices from `CachingModelChoicesFormSet` and uses them in model choice fields in order to reduce
    number of DB queries when used in admin inlines.
    """

    @property
    def model_choice_fields(self):
        return [fn for fn, f in self.fields.items()
            if isinstance(f, (forms.ModelChoiceField, forms.ModelMultipleChoiceField,))]

    def __init__(self, *args, **kwargs):
        cached_choices = kwargs.pop('cached_choices', {})
        super().__init__(*args, **kwargs)
        for field_name, choices in cached_choices.items():
            if choices is not None and field_name in self.fields:
                self.fields[field_name].choices = choices

您需要做的就是从CachingModelChoicesForm继承您的模型,并在您的内联类中使用CachingModelChoicesFormSet:

class ArrangementInlineForm(CachingModelChoicesForm):
    class Meta:
        model = Arrangement
        exclude = ()


class ArrangementInline(admin.TabularInline):
    model = Arrangement
    extra = 50
    form = ArrangementInlineForm
    formset = CachingModelChoicesFormSet

答案 1 :(得分:2)

这在Django中是非常正常的行为 - 它不会为你做优化,但它为你自己提供了不错的工具。并且不要冒汗,100个查询并不是一个大问题(我在一个页面上看到了16k查询),需要立即修复。但是如果你的数据量会迅速增加,那么当然应该处理它是明智的。

您将使用的主要武器是查询集方法select_related()prefetch_related()。真的没有必要深入研究它们,因为它们有很好的记录here,但只是一般指针:

  • 当您查询的对象只有一个相关对象(FK或one2one)时使用select_related()

  • 当您查询的对象有多个相关对象(FK或M2M的另一端)时使用prefetch_related()

如何在Django管理员中使用它们,你问?小学,亲爱的沃森。覆盖管理页面方法get_queryset(self, request),所以它看起来像这样:

from django.contrib import admin

class SomeRandomAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('field1', 'field2').prefetch_related('field3')    

编辑:阅读完您的评论后,我意识到我对您的问题的初步解释是绝对错误的。我也有针对您的问题的多种解决方案,这就是:

  1. 我大部分时间都在使用的简单建议:只需用raw_id_field小部件替换Django默认选择小部件,不会进行任何查询。只需在内联管理员中设置raw_id_fields = ('setting', 'displayed_group')即可。

  2. 但是,如果你不想摆脱选择框,我可以提供一些半hacky代码来完成这个技巧,但是相当冗长且不太漂亮。我们的想法是覆盖创建表单的formset,并为formset中的这些字段指定选项,以便它们只从数据库中查询一次。

  3. 这就是:

    from django import forms
    from django.contrib import admin
    from app.models import Arrangement, Machine, Setting, DisplayedGroup
    
    
    class ChoicesFormSet(forms.BaseInlineFormSet):
        setting_choices = list(Setting.objects.values_list('id', 'name'))
        displayed_group_choices = list(DisplayedGroup.objects.values_list('id', 'name'))
    
        def _construct_form(self, i, **kwargs):
            kwargs['setting_choices'] = self.setting_choices
            kwargs['displayed_group_choices'] = self.displayed_group_choices
            return super()._construct_form(i, **kwargs)
    
    
    class ArrangementInlineForm(forms.ModelForm):
        class Meta:
            model = Arrangement
            exclude = ()
    
        def __init__(self, *args, **kwargs):
            setting_choices = kwargs.pop('setting_choices', [((), ())])
            displayed_group_choices = kwargs.pop('displayed_group_choices', [((), ())])
    
            super().__init__(*args, **kwargs)
    
            # This ensures that you can still save the form without setting all 50 (see extra value) inline values.
            # When you save, the field value is checked against the "initial" value
            # of a field and you only get a validation error if you've changed any of the initial values.
            self.fields['setting'].choices = [('-', '---')] + setting_choices
            self.fields['setting'].initial = self.fields['setting'].choices[0][0]
            self.fields['setting'].empty_values = (self.fields['setting'].choices[0][0],)
    
            self.fields['displayed_group'].choices = displayed_group_choices
            self.fields['displayed_group'].initial = self.fields['displayed_group'].choices[0][0]
    
    
    class ArrangementInline(admin.TabularInline):
        model = Arrangement
        extra = 50
        form = ArrangementInlineForm
        formset = ChoicesFormSet
    
        def get_queryset(self, request):
            return super().get_queryset(request).select_related('setting')
    
    
    class MachineAdmin(admin.ModelAdmin):
        inlines = (ArrangementInline,)
    
    
    admin.site.register(Machine, MachineAdmin)
    

    如果您发现可以改进的内容或有任何疑问,请与我们联系。

答案 2 :(得分:0)

现在,(对that question表示敬意),BaseFormset收到form_kwargs attribute

可以对接受的答案中的ChoicesFormSet代码进行如下修改:

class ChoicesFormSet(forms.BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        setting_choices = list(Setting.objects.values_list('id', 'name'))
        displayed_group_choices = list(DisplayedGroup.objects.values_list('id', 'name'))
        self.form_kwargs['setting_choices'] = self.setting_choices
        self.form_kwargs['displayed_group_choices'] = self.displayed_group_choices

其余代码保持完整,如公认的答案所述:

class ArrangementInlineForm(forms.ModelForm):
    class Meta:
        model = Arrangement
        exclude = ()

    def __init__(self, *args, **kwargs):
        setting_choices = kwargs.pop('setting_choices', [((), ())])
        displayed_group_choices = kwargs.pop('displayed_group_choices', [((), ())])

        super().__init__(*args, **kwargs)

        # This ensures that you can still save the form without setting all 50 (see extra value) inline values.
        # When you save, the field value is checked against the "initial" value
        # of a field and you only get a validation error if you've changed any of the initial values.
        self.fields['setting'].choices = [('-', '---')] + setting_choices
        self.fields['setting'].initial = self.fields['setting'].choices[0][0]
        self.fields['setting'].empty_values = (self.fields['setting'].choices[0][0],)

        self.fields['displayed_group'].choices = displayed_group_choices
        self.fields['displayed_group'].initial = self.fields['displayed_group'].choices[0][0]


class ArrangementInline(admin.TabularInline):
    model = Arrangement
    extra = 50
    form = ArrangementInlineForm
    formset = ChoicesFormSet

    def get_queryset(self, request):
        return super().get_queryset(request).select_related('setting')


class MachineAdmin(admin.ModelAdmin):
    inlines = (ArrangementInline,)


admin.site.register(Machine, MachineAdmin)