Django Class Based View: Validate object in dispatch

三世轮回 提交于 2019-12-05 21:35:13

You can cache the result of get_object().

Here's a trivial example:

class CourseUpdateView(UpdateView):
    # [...] your dispatch method

    def get_object(self):
        # it doesn't matter how many times get_object is called per request
        # it should not do more than one request
        if not hasattr(self, '_object'):
            self._object = super(CourseUpdateView, self).get_object()
        return self._object
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!