Django Model Formset在POST时非常慢

mw3dktmi  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(91)

我试图允许用户在一个屏幕上修改多个记录,所以Django Formsets似乎是最好的选择。表单集中的每个表单包含9个外键字段,这导致运行许多查询。我通过使用Jquery Autocomplete解决了表单渲染方面的问题,这将渲染时间从30或40秒减少到大约3或4秒(将数百个查询减少到4个)。我现在遇到的性能问题是在POST端--当表单提交时,根据Django Debug Toolbar,它仍然运行数百个重复的查询。我花了几个小时阅读表单集文档和各种博客和这里的建议。我尝试的最后一件事是将表单集queryset设置为自定义查询,并为每个ForeignKey字段使用select_related()。根据DjDT,这似乎使主查询包含了大量的连接,但它并没有消除POST上剩余的数百个查询。我已经把头发都拔光了,所以也许有人有建议或者能看出我做错了什么?
我的模特:

class Info(AbstractAddArchive):
    item= models.ForeignKey(ITEM, on_delete=models.PROTECT)
    rec= models.ForeignKey(Log, on_delete=models.PROTECT, null=True, blank=True)
    c1 = models.CharField(max_length=2, blank=True, null=True)
    c1rec = models.ForeignKey(Log, on_delete=models.PROTECT, related_name='c1rec', blank=True, null=True)
    c1sts = models.ForeignKey(Sts, on_delete=models.PROTECT, null=True, blank=True)
    c1f = models.IntegerField(blank=True, null=True)
    c2 = models.CharField(max_length=2, blank=True, null=True)
    c2rec = models.ForeignKey(Log, on_delete=models.PROTECT, related_name='c2rec', blank=True, null=True)
    c2sts = models.ForeignKey(Sts, on_delete=models.PROTECT, null=True, blank=True, related_name='c2sts')
    c2f = models.IntegerField(blank=True, null=True)
    c3 = models.CharField(max_length=2, blank=True, null=True)
    c3rec = models.ForeignKey(Log, on_delete=models.PROTECT, related_name='c3rec', blank=True, null=True)
    c3sts = models.ForeignKey(Sts, on_delete=models.PROTECT, null=True, blank=True, related_name='c3sts')
    c3f = models.IntegerField(blank=True, null=True)
    c4 = models.CharField(max_length=2, blank=True, null=True)
    c4rec = models.ForeignKey(Log, on_delete=models.PROTECT, related_name='c4rec', blank=True, null=True)
    c4sts = models.ForeignKey(Sts, on_delete=models.PROTECT, null=True, blank=True, related_name='c4sts')
    c4 = models.IntegerField(blank=True, null=True)
    comment1 = models.CharField(max_length=45, blank=True, null=True)
    comment2 = models.CharField(max_length=45, blank=True, null=True)
    posted_by = models.ForeignKey(User, on_delete=models.PROTECT, max_length=10, null=True, blank=True)
    posted_ts = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        ordering = ('item',)  # This sets initial order of formset display

    def __str__(self):
        return f"{self.item_id}"

class Log(AbstractAddArchive):
    rec= models.CharField(max_length=10, primary_key=True)
    ... other fields ...
    slug = models.SlugField(null=False, blank=True, unique=True)

    def __str__(self):
            return f"{self.name}"

class Sts(models.Model):
    name = models.CharField(max_length=10, primary_key=True)
    description = models.CharField(max_length=50, null=True, blank=True)
    sequence = models.IntegerField()

    def __str__(self):
        return f"{self.name}"

字符串
我的表单和表单集:

class InfoReviseForm(forms.ModelForm):
    class Meta:
        model = Info
        fields = ('rec',
                  'c1rec',
                  'c1f',
                  'c1sts',
                  'c2rec',
                  'c2f',
                  'c2sts',
                  'c3rec',
                  'c3f',
                  'c3sts',
                  'c4rec',
                  'c4f',
                  'c4sts',
                  'comment1',
                  )
        widgets = {
            'rec': forms.TextInput(attrs={
                'class': 'autocomplete',       
                'placeholder': 'Choose rec...',
                'app': 'appname',
                'model-class': 'Log',
                'search-field': 'rec',
                'autocomplete-url': reverse_lazy('autocomplete_field')}),
            ... similar for all foreignkey fields - .js enables autocomplete on each
            }

class InfoBaseFormSet(forms.BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.queryset = Info.objects.select_related('rec').select_related('c1rec').select_related('c2rec').select_related('c3rec').select_related('c4rec').select_related('car').select_related('c1sts').select_related('c2sts').select_related('c3sts').select_related('c4sts')

InfoReviseFormset = modelformset_factory(Info, form=InfoReviseForm, extra=0, formset=InfoBaseFormSet)


我的观点(岗位方法)

def post(self, request, *args, **kwargs):
        formset = InfoReviseFormset(request.POST)
        if formset.is_valid():
            for form in formset:
                if form.has_changed():
                    carinfo_active = form.instance
                    carinfo_active.posted_by = request.user
                    carinfo_active.save()  #save new data - updates existing non archive record
            return super().post(request, *args, **kwargs)


据我所知,“if formset.is_valid”是很多查询运行的地方--但在那之后它也很慢。顺便说一句,目前我有大约55个表单在表单集中-表单的最大数量可能高达200,这只会减慢事情更多。如果我能减少查询,我认为这将更合理地运行。
我简化了上面的一些代码--如果缺少任何东西来帮助查看,我很乐意发布更多。
谢谢你的任何建议

n9vozmp4

n9vozmp41#

性能不好的可能是formset.is_valid()相关的View和您的查询集相关。尝试将查询集定义从窗体的类定义移动到视图中的显式标注。

def post(self, request, *args, **kwargs):
    myquery = Info.objects.select_related('rec').select_related('c1rec').select_related('c2rec').select_related('c3rec').select_related('c4rec').select_related('car').select_related('c1sts').select_related('c2sts').select_related('c3sts').select_related('c4sts')
    formset = InfoReviseFormset(request.POST, queryset=myquery)
    if formset.is_valid():
        for form in formset:
            if form.has_changed():
                carinfo_active = form.instance
                carinfo_active.posted_by = request.user
                carinfo_active.save()  #save new data - updates existing non archive record
        return super().post(request, *args, **kwargs)

字符串
在类似的情况下,我发现随着项目中模型示例总数的增加,我的保存时间变得越来越长。为了调试这个问题,我测量了每行代码的时间,并将问题隔离到formset.is_valid()。通过添加显式查询集,每个表单集的保存时间从10秒减少到不到半秒。
范例:

assyform = AssyModelFormset(request.POST, request.FILES, prefix='assy', queryset=assembly_query)

相关问题