• drf获取请求过来时的request


    0. 获取请求过来时的request

    """
    
    获取请求时带过来的request,结合drf认证组件,判断用户的登录状态,
    
    来获取用户的粒度操作,比如点赞/收藏/关注 的取消与私有化,
    
    示例:用在序列化中的字段钩子方法
    
    """
    from rest_framework import serializers
    from rest_framework.generics import ListAPIView
    from api.serializer.articlelist import ArticleListSerializer
    from api import models
    
    
    
    class ArticleSerializer(serializers.ModelSerializer):
    
        faver = serialiers.SerializerMethodField()
        
        class Meta:
            model = models.ArticleDetail
            fields = "__all__"
            
        def get_faver(self,obj):
            
            request = self.context["request"]
            
            if not request.user:
                return False
            exists = models.ArticleDetail.objects
            
    
    
    
    class ArticleListView(ListAPIView):
        queryset = models.ArticleDetail.objects
        serializer_class = ArticleListSerializer
    

    1. get方法(ListAPIView)

    class ListAPIView(mixins.ListModelMixin,
                  GenericAPIView):
    	"""
    	Concrete view for listing a queryset.
    	"""
    	def get(self, request, *args, **kwargs):
    		return self.list(request, *args, **kwargs)
    

    2. list方法(ListModelMixin)

    class ListModelMixin:
    	"""
    	List a queryset.
    	"""
    	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)
    

    3. get_queryset方法(GenericAPIView)

    class GenericAPIView(views.APIView):
    
        queryset = None
        serializer_class = None
        
        def get_queryset(self):
        
            assert self.queryset is not None, (
                "'%s' should either include a `queryset` attribute, "
                "or override the `get_queryset()` method."
                % self.__class__.__name__
            )
    
            queryset = self.queryset
            if isinstance(queryset, QuerySet):
                # Ensure queryset is re-evaluated on each request.
                queryset = queryset.all()
            return queryset
    

    4. get_serializer(GenericAPIView)

    def get_serializer(self, *args, **kwargs):
        """
        Return the serializer instance that should be used for validating and
        deserializing input, and for serializing output.
        """
        serializer_class = self.get_serializer_class()
        
        kwargs['context'] = self.get_serializer_context()
        
        return serializer_class(*args, **kwargs)
    

    5. get_serializer_class(GenericAPIView)

    def get_serializer_class(self):
        """
        Return the class to use for the serializer.
        Defaults to using `self.serializer_class`.
    
        You may want to override this if you need to provide different
        serializations depending on the incoming request.
    
        (Eg. admins get full serialization, others get basic serialization)
        """
        assert self.serializer_class is not None, (
            "'%s' should either include a `serializer_class` attribute, "
            "or override the `get_serializer_class()` method."
            % self.__class__.__name__
        )
    
        return self.serializer_class
    

    6. get_serializer_context(GenericAPIView)

    def get_serializer_context(self):
        """
        Extra context provided to the serializer class.
        """
        return {
            'request': self.request,
            'format': self.format_kwarg,
            'view': self
        }
    希望你眼眸有星辰,心中有山海,从此以梦为马,不负韶华
  • 相关阅读:
    STP配置和选路规则
    Apache(基于端口号)
    Apache(基于主机名)
    Apache(基于IP地址)
    (Apache服务)个人用户主页功能
    c语言实现通讯录,包括增加名字,删除信息,查找,修改,排序
    C语言用函数指针的数组简单实现计算器功能
    指针与数组,指针与函数之间的关系
    循环
    SQLite不支持的SQL语法总结
  • 原文地址:https://www.cnblogs.com/daviddd/p/12202498.html
Copyright © 2020-2023  润新知