码迷,mamicode.com
首页 > 其他好文 > 详细

drf获取请求过来时的request

时间:2020-01-16 19:26:02      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:form   for   user   code   inpu   instance   认证   lists   钩子方法   

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

标签:form   for   user   code   inpu   instance   认证   lists   钩子方法   

原文地址:https://www.cnblogs.com/daviddd/p/12202498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!