如何优化Django FormView以获得数据库性能

时间:2018-06-03 10:48:10

标签: python django django-models django-forms

我从来没有优化过我的Django代码,而且我不确定我是否完全理解Django optimisation docs所以你能否告诉我这个FormView类是否能以某种方式进行优化(我猜是的......) )?

我担心的代码部分是患者查找:Patient.objects.get(patientId=self.kwargs['patientId']) - 它发生了3次......这是否意味着 Django会在数据库中出现3次或只有一次?

可以/应该优化,如果是 - 如何?

class PatientNotes(SingleObjectMixin, FormView):

    slug_url_kwarg = 'patientId'
    slug_field = 'patientId'
    pk_url_kwarg = 'patientId'

    template_name = "patient/patient_detail.html"
    form_class = AddNewNoteForm

    def get_queryset(self):
        queryset = super(PatientNotes, self).get_queryset()
        self.current_patient = Patient.objects.get(patientId=self.kwargs['patientId'])
        my_result = queryset.filter(patient=self.current_patient)
        return my_result

    def post(self, request, *args, **kwargs):
        self.object = Patient.objects.get(patientId=self.kwargs['patientId'])
        return super().post(request, *args, **kwargs)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.patient = Patient.objects.get(patientId=self.kwargs['patientId'])
        self.object.note_created_by_date = datetime.date.today()
        self.object.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('PatientDetailView', kwargs={'patientId': self.object.patient.patientId})

1 个答案:

答案 0 :(得分:0)

这意味着您的视图包含将多次命中数据库的重复查询。

使用

class PatientNotes(SingleObjectMixin, FormView):
    ...

    def get_queryset(self):
        ...
        # This will hit the database to fetch patient object.
        self.current_patient = Patient.objects.get(patientId=self.kwargs['patientId'])
        ...

    def post(self, request, *args, **kwargs):
        # This will also hit the database to fetch patient object.
        self.object = Patient.objects.get(patientId=self.kwargs['patientId'])
        ...

    def form_valid(self, form):
        ...
        # This will also hit the database to fetch patient object.
        self.object.patient = Patient.objects.get(patientId=self.kwargs['patientId'])
        ...

这被视为在单个请求期间进行的重复查询。

要解决此问题,请考虑更新getpost方法,在FormView不需要的情况下,将当前对象设置为None get请求在保存表单后,post方法更新现有对象将设置当前对象。

我建议您使用UpdateViewCreateView,因为您尝试设置self.object,即FormView以来的当前对象用于处理表单,即仅验证并保存表单数据。

模型字段不应该是驼峰式Patient.patientId应该是Patient.patient_id它们应该是低位的,并用下划线分隔。

class PatientNotes(SingleObjectMixin, FormView):
    model = Patient
    slug_url_kwarg = 'patient_id'
    slug_field = 'patient_id'
    pk_url_kwarg = 'patient_id'
    template_name = 'patient/patient_detail.html'
    form_class = AddNewNoteForm

    def get(self, request, *args, **kwargs):
        self.patient_object = self.get_object()
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.patient_object = self.get_object()
        return super().post(request, *args, **kwargs)

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.patient = self.patient_object
        self.object.note_created_by_date = datetime.date.today()
        self.object.save()
        return super().form_valid(form)

    def get_success_url(self):
        # view names should be lower cased separated by underscores.
        return reverse('patient_detail_view', kwargs={'patient_id': self.patient_object.patient_id})