drf获取请求过来时的request

Posted daviddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
    }
    
    

    
    
    
    
    
    
    
    
    
    
    
    

以上是关于drf获取请求过来时的request的主要内容,如果未能解决你的问题,请参考以下文章

DRF的解析器和渲染器

DRF教程7-认证

DRF的解析器和渲染器

drf--视图,路由

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段

DRF请求响应组件