django drf 统一处理操作人和时间字段

场景

如果你的表结构有这些字段:创建人/创建时间/更新人/更新时间/删除人/删除时间,我们可以统一处理这些字段的更新和插入,而不需要额外显示操作。

代码

1.ActionViewSetMixin:

python 复制代码
import datetime
from rest_framework import status
from rest_framework.response import Response



class ActionViewSetMixin:

   def destroy_mixin(self, request):
       instance = self.get_object()
       data = {
           "deleted_by": request.user.username,
           "deleted_at": datetime.datetime.now(),
           "is_deleted": True,
       }
       serializer = self.get_serializer(instance, data=data, partial=True)
       serializer.is_valid(raise_exception=True)
       self.perform_update(serializer)

       if getattr(instance, "_prefetched_objects_cache", None):
           # If 'prefetch_related' has been applied to a queryset, we need to
           # forcibly invalidate the prefetch cache on the instance.
           instance._prefetched_objects_cache = {}

       return Response(serializer.data)

   def destroy(self, request, *args, **kwargs):
       return self.destroy_mixin(request)

   def update_mixin(self, request, serializer):
       serializer.is_valid(raise_exception=True)
       data = serializer.validated_data
       data["updated_by"] = request.user.username
       data["updated_at"] = datetime.datetime.now()
       partial = True
       instance = self.get_object()
       serializer = self.get_serializer(instance, data=data, partial=partial)
       serializer.is_valid(raise_exception=True)
       self.perform_update(serializer)

       if getattr(instance, "_prefetched_objects_cache", None):
           # If 'prefetch_related' has been applied to a queryset, we need to
           # forcibly invalidate the prefetch cache on the instance.
           instance._prefetched_objects_cache = {}

       return Response(serializer.data)

   def update(self, request, *args, **kwargs):
       serializer_class = kwargs.pop("serializer", self.get_serializer)
       return self.update_mixin(request, serializer_class(data=request.data))

   def create_mixin(self, request, *args, **kwargs):
       data = request.data.copy()
       data["created_by"] = request.user.username
       data["created_at"] = datetime.datetime.now()
       serializer_class = kwargs.pop("serializer", self.get_serializer)
       serializer = serializer_class(data=data)
       serializer.is_valid(raise_exception=True)
       self.perform_create(serializer)
       headers = self.get_success_headers(serializer.data)
       return Response(
           serializer.data, status=status.HTTP_201_CREATED, headers=headers
       )

   def create(self, request, *args, **kwargs):
       return self.create_mixin(request, *args, **kwargs)

2.APIModelViewSet继承ModelViewSet和ActionViewSetMixin:

python 复制代码
from rest_framework.viewsets import ModelViewSet

from common.drf.mixins import ActionViewSetMixin, ResponseMixin


class APIModelViewSet(ActionViewSetMixin, ResponseMixin, ModelViewSet):
   extra_permissions = []  # 不覆盖默认的权限
   action_extra_permission_mapping = {}

   def parse_page_params(self):
       if self.request.method == "GET":
           page = self.request.query_params.get("page", 1)
           page_size = self.request.query_params.get("page_size", 10)
       else:
           page = self.request.data.get("page", 1)
           page_size = self.request.data.get("page_size", 10)
       try:
           return int(page), int(page_size)
       except Exception as e:
           return 1, 10

   def get_permissions(self):
       permissions = super(APIModelViewSet, self).get_permissions()

       extra = [x() for x in self.extra_permissions]
       action = self.action_extra_permission_mapping.get(self.action, [])
       return permissions + extra + [p() for p in action]

   @classmethod
   def get_validated_params(cls, serializer, params):
       s = serializer(data=params)
       s.is_valid(raise_exception=True)
       return s.validated_data

3.视图类继承APIModelViewSet:

python 复制代码
class ScanRecordViewSet(APIModelViewSet):
   queryset = models.ScanRecord.exclude_deleted_objects.all()
   serializer_class = serializers.ScanRecordSerializer
   pagination_class = DataPageNumberPagination
   filter_backends = [CustomFilterBackend, SearchFilter]

   search_fields = ["status", "mode", 'risk']
   custom_query_fields = ["status", "mode", "risk"]
相关推荐
算法小白(真小白)3 小时前
低代码软件搭建自学第二天——构建拖拽功能
python·低代码·pyqt
唐小旭3 小时前
服务器建立-错误:pyenv环境建立后python版本不对
运维·服务器·python
007php0073 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
Chinese Red Guest3 小时前
python
开发语言·python·pygame
骑个小蜗牛4 小时前
Python 标准库:string——字符串操作
python
黄公子学安全6 小时前
Java的基础概念(一)
java·开发语言·python
程序员一诺6 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python
小木_.7 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
isSamle7 小时前
使用Vue+Django开发的旅游路书应用
前端·vue.js·django