Django 模型表单集性能
Django model formset performance
我对 Django 1.4.2 和 PostgreSQL 9.1 的性能问题不大。我想使用这样创建的表单从集创建模型:
forms.py
class AcknowledgeForm(forms.ModelForm):
class Meta:
model = Attendance
fields = ['acknowledge', ]
widgets = {'acknowledge': forms.CheckboxInput()}
AcknowledgeFormset = forms.models.modelformset_factory(Attendance, form=AcknowledgeForm, extra=0)
对于外键很少的模型
models.py
class Attendance(models.Model): #500k rows in DB
zamestnani = models.ForeignKey('people.Zamestnani', related_name='attendance') #~1k rows
day = models.ForeignKey(Day) #~2.5k rows
acknowledge = models.NullBooleanField(blank=True, null=True)
views.py
class VacationAcknowledgeView(LoginRequiredMixin, TemplateView):
template_name = "presence/presence_vacation_acknowledge.html"
http_method_names = ['get', 'post']
def get_context_data(self, **kwargs):
context = super(VacationAcknowledgeView, self).get_context_data()
person = Person.fromRequest(self.request)
first_day = date(date.today().year, 1, 1)
days = Attendance.objects.filter(acknowledge=None, day__date__gte=first_day, zamestnani__osoba=person)
context['formset'] = AcknowledgeFormset(queryset=days)
return context
def post(self, request, *args, **kwargs):
#next line is screwed
formset = AcknowledgeFormset(request.POST)
#never been there....
return super(VacationAcknowledgeView, self).get(request, *args, **kwargs)
我可以创建它,渲染它,一切似乎都正常,但是从 POST 分配数据会导致单个对象的服务器冻结很长的时间(几小时)。
经过短暂的挖掘,我个人责怪模型表单集,因为当我创建它并像处理单个表单一样处理它时,一切都按预期工作。但是我不知道如何 fix/evade 这个。
感谢任何合理的建议。
您提到 Attendance
模型在您的数据库中有大约 500,000 行。在 Django 中,当没有为 ModelFormSet
指定 queryset
参数时,它包括该模型中的所有对象。 Django 可能正在获取所有 500,000 行数据。
您需要找出 ModelFormSet
的查询集。例如
def post(self, request, *args, **kwargs):
queryset = Attendance.objects.none()
formset = AcknowledgeFormset(queryset=queryset, data=request.POST)
# continue with your regular code execution
文档包含 ModelFormSet
的查询集部分:https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#django.forms.models.BaseModelFormSet
我对 Django 1.4.2 和 PostgreSQL 9.1 的性能问题不大。我想使用这样创建的表单从集创建模型:
forms.py
class AcknowledgeForm(forms.ModelForm):
class Meta:
model = Attendance
fields = ['acknowledge', ]
widgets = {'acknowledge': forms.CheckboxInput()}
AcknowledgeFormset = forms.models.modelformset_factory(Attendance, form=AcknowledgeForm, extra=0)
对于外键很少的模型
models.py
class Attendance(models.Model): #500k rows in DB
zamestnani = models.ForeignKey('people.Zamestnani', related_name='attendance') #~1k rows
day = models.ForeignKey(Day) #~2.5k rows
acknowledge = models.NullBooleanField(blank=True, null=True)
views.py
class VacationAcknowledgeView(LoginRequiredMixin, TemplateView):
template_name = "presence/presence_vacation_acknowledge.html"
http_method_names = ['get', 'post']
def get_context_data(self, **kwargs):
context = super(VacationAcknowledgeView, self).get_context_data()
person = Person.fromRequest(self.request)
first_day = date(date.today().year, 1, 1)
days = Attendance.objects.filter(acknowledge=None, day__date__gte=first_day, zamestnani__osoba=person)
context['formset'] = AcknowledgeFormset(queryset=days)
return context
def post(self, request, *args, **kwargs):
#next line is screwed
formset = AcknowledgeFormset(request.POST)
#never been there....
return super(VacationAcknowledgeView, self).get(request, *args, **kwargs)
我可以创建它,渲染它,一切似乎都正常,但是从 POST 分配数据会导致单个对象的服务器冻结很长的时间(几小时)。
经过短暂的挖掘,我个人责怪模型表单集,因为当我创建它并像处理单个表单一样处理它时,一切都按预期工作。但是我不知道如何 fix/evade 这个。
感谢任何合理的建议。
您提到 Attendance
模型在您的数据库中有大约 500,000 行。在 Django 中,当没有为 ModelFormSet
指定 queryset
参数时,它包括该模型中的所有对象。 Django 可能正在获取所有 500,000 行数据。
您需要找出 ModelFormSet
的查询集。例如
def post(self, request, *args, **kwargs):
queryset = Attendance.objects.none()
formset = AcknowledgeFormset(queryset=queryset, data=request.POST)
# continue with your regular code execution
文档包含 ModelFormSet
的查询集部分:https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/#django.forms.models.BaseModelFormSet