Django - 在 POST 中解析表单集数据非常慢
Django - Parsing formset data in POST is painfully slow
我有一个模型:
class IP(models.Model):
address = models.GenericIPAddressField()
fqdn = models.CharField(max_length=255, blank=True)
description = models.CharField(max_length=1000, blank=True)
available = models.BooleanField(default=True)
reserved = models.BooleanField(default=False)
network = models.ForeignKey(Network, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer,on_delete=models.CASCADE, blank=True, null=True)
和一个表格:
class IPForm(ModelForm):
id = forms.CharField(widget=forms.HiddenInput())
address = forms.CharField(disabled=True,
widget=forms.TextInput(attrs={'class':' form-control'}))
fqdn = forms.CharField(widget=forms.TextInput(attrs={'class':' form-control'}), required=False)
description = forms.CharField(widget=forms.TextInput(attrs={'class':' form-control ip_description'}), required = False)
customer = forms.ModelChoiceField(queryset=Customer.objects.all().order_by('name'), widget=forms.Select(attrs={'class':'form-control'}), required=False)
reserved = forms.BooleanField(widget=forms.CheckboxInput(attrs= {'class':'checkbox'}), required=False)
class Meta:
model = IP
exclude = ['available', 'network']
和一个使用 modelformset_factory:
创建表单集的视图
def ipDetailView(request, ipIDs=None):
ipformset = modelformset_factory(IP, form=IPForm, extra=0)
if request.method == 'POST':
if len(request.POST.getlist('ip')):
#we were sent a list of IP IDs to edit
if 'clear_btn' in request.POST:
#just remove the fqdn/description of the posted IPs
ips = IP.objects.filter(id__in=request.POST.getlist('ip'))
for ip in ips:
ip.fqdn = ''
ip.description = ''
ip.available = True
ip.save()
return redirect ('iplistview', networkID=ips[0].network.id)
else:
#post the forms so the IPs can be edited
formset = ipformset(queryset=IP.objects.filter(id__in=request.POST.getlist('ip')))
return render (request, 'ipmanager/ipdetailview.html', {'formset' : formset})
else:
#we were sent a set of IP forms to commit changes for
formset = ipformset(request.POST)
if formset.is_valid():
instances = formset.save()
return redirect ('iplistview', networkID=instances[0].network.id)
else:
return render(request, 'ipmanager/ipdetailview.html',
{
'formset' : formset,
})
else:
network = IP.objects.filter(id=ipIDs)[0].network
formset = ipformset(queryset=IP.objects.filter(id=ipIDs))
return render (request, 'ipmanager/ipdetailview.html', {'formset' : formset, 'network' : network})
我的问题是,无论我是提交 1 个 IP 还是 20 个 IP,我提交 IP 表单更改的 POST 部分需要 5-8 秒才能提交。代码非常简单那部分:
formset = ipformset(request.POST)
if formset.is_valid():
instances = formset.save()
我什至不确定如何弄清楚为什么要花这么长时间。我没有似乎陷入其他类似问题的多对多关系。我如何着手找出导致速度缓慢的原因?
你可以使用django-debug-toolbar to identify what queries are taking most time, the raw SQL statement, where in your code it was called, and EXPLAIN
它。
它还可以检测可以避免的潜在重复查询。
如果您注意到有重复的查询,通常可以使用 select_related
.
来避免它们
我有一个模型:
class IP(models.Model):
address = models.GenericIPAddressField()
fqdn = models.CharField(max_length=255, blank=True)
description = models.CharField(max_length=1000, blank=True)
available = models.BooleanField(default=True)
reserved = models.BooleanField(default=False)
network = models.ForeignKey(Network, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer,on_delete=models.CASCADE, blank=True, null=True)
和一个表格:
class IPForm(ModelForm):
id = forms.CharField(widget=forms.HiddenInput())
address = forms.CharField(disabled=True,
widget=forms.TextInput(attrs={'class':' form-control'}))
fqdn = forms.CharField(widget=forms.TextInput(attrs={'class':' form-control'}), required=False)
description = forms.CharField(widget=forms.TextInput(attrs={'class':' form-control ip_description'}), required = False)
customer = forms.ModelChoiceField(queryset=Customer.objects.all().order_by('name'), widget=forms.Select(attrs={'class':'form-control'}), required=False)
reserved = forms.BooleanField(widget=forms.CheckboxInput(attrs= {'class':'checkbox'}), required=False)
class Meta:
model = IP
exclude = ['available', 'network']
和一个使用 modelformset_factory:
创建表单集的视图def ipDetailView(request, ipIDs=None):
ipformset = modelformset_factory(IP, form=IPForm, extra=0)
if request.method == 'POST':
if len(request.POST.getlist('ip')):
#we were sent a list of IP IDs to edit
if 'clear_btn' in request.POST:
#just remove the fqdn/description of the posted IPs
ips = IP.objects.filter(id__in=request.POST.getlist('ip'))
for ip in ips:
ip.fqdn = ''
ip.description = ''
ip.available = True
ip.save()
return redirect ('iplistview', networkID=ips[0].network.id)
else:
#post the forms so the IPs can be edited
formset = ipformset(queryset=IP.objects.filter(id__in=request.POST.getlist('ip')))
return render (request, 'ipmanager/ipdetailview.html', {'formset' : formset})
else:
#we were sent a set of IP forms to commit changes for
formset = ipformset(request.POST)
if formset.is_valid():
instances = formset.save()
return redirect ('iplistview', networkID=instances[0].network.id)
else:
return render(request, 'ipmanager/ipdetailview.html',
{
'formset' : formset,
})
else:
network = IP.objects.filter(id=ipIDs)[0].network
formset = ipformset(queryset=IP.objects.filter(id=ipIDs))
return render (request, 'ipmanager/ipdetailview.html', {'formset' : formset, 'network' : network})
我的问题是,无论我是提交 1 个 IP 还是 20 个 IP,我提交 IP 表单更改的 POST 部分需要 5-8 秒才能提交。代码非常简单那部分:
formset = ipformset(request.POST)
if formset.is_valid():
instances = formset.save()
我什至不确定如何弄清楚为什么要花这么长时间。我没有似乎陷入其他类似问题的多对多关系。我如何着手找出导致速度缓慢的原因?
你可以使用django-debug-toolbar to identify what queries are taking most time, the raw SQL statement, where in your code it was called, and EXPLAIN
它。
它还可以检测可以避免的潜在重复查询。
如果您注意到有重复的查询,通常可以使用 select_related
.