我从来没有优化过我的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})
https://stackoverflow.com/questions/50665548
复制相似问题