django 中使得缓存方法

    def latest_entry(request,  *args, **kwargs):

        from urllib.parse import urlparse

        # print('latest_entry >>>', request, args, kwargs)

        print(request.get_full_path_info())

        try:

            p = urlparse(request.get_full_path_info())

            project_id = None

            for rec in p.query.split('&'):

                items = str(rec).split('=')

                if items[0] == 'project':

                    project_id = items[1]

                    break

            print('find project id >>>', project_id)

            pro = m.Project.objects.get(id=project_id)

            if pro:

                print('pro.date_updated >>>',pro.date_updated)

                return pro.date_updated

            return datetime.now()

        except:

            print('latest_entry got exception!!!')

            traceback.print_exc()

            return datetime.now()

# 由于list函数第一个参数是self,所以要再包一层。list函数是mixins.ListModelMixin的方法

    @method_decorator(condition(last_modified_func=latest_entry) )

    def list(self, request, *args, **kwargs):

        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)

        if page is not None:

            serializer = self.get_serializer(page, many=True)

            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)

        return Response(serializer.data)



参考:

https://docs.djangoproject.com/zh-hans/4.1/topics/conditional-view-processing/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容