首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何优化Django FormView以提高数据库性能

如何优化Django FormView以提高数据库性能
EN

Stack Overflow用户
提问于 2018-06-03 18:48:10
回答 1查看 45关注 0票数 0

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

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

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

代码语言:javascript
运行
复制
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})
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50665548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档