专栏首页dongfangerDRF类视图让你的代码DRY起来

DRF类视图让你的代码DRY起来

刚开始写views.py模块的代码,一般都是用def定义的函数视图,不过DRF更推荐使用class定义的类视图,这能让我们的代码更符合DRY(Don't Repeat Yourself)设计原则:

使用APIView

rest_framework.views.APIView是DRF封装的API视图,继承了django.views.generic.base.View

我们用它把函数视图改写成类视图,编辑snippets/views.py

from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status


class SnippetList(APIView):
    """
    List all snippets, or create a new snippet.
    """
    def get(self, request, format=None):
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = SnippetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    
    
class SnippetDetail(APIView):
    """
    Retrieve, update or delete a snippet instance.
    """
    def get_object(self, pk):
        try:
            return Snippet.objects.get(pk=pk)
        except Snippet.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = SnippetSerializer(snippet)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        snippet = self.get_object(pk)
        serializer = SnippetSerializer(snippet, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        snippet = self.get_object(pk)
        snippet.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

类视图的代码跟函数视图是非常类似的,区别在于GETPOST等方法是用的函数而不是if语句,可以更好的解耦代码。

改了views.py代码后,需要同时修改snippets/urls.py

from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from snippets import views

urlpatterns = [
    path('snippets/', views.SnippetList.as_view()),
    path('snippets/<int:pk>/', views.SnippetDetail.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)

为什么要加个as_view()方法?

因为path()的参数必须是可调用的,在源码中能看到elif callable(view)

def _path(route, view, kwargs=None, name=None, Pattern=None):
    if isinstance(view, (list, tuple)):
        # For include(...) processing.
        pattern = Pattern(route, is_endpoint=False)
        urlconf_module, app_name, namespace = view
        return URLResolver(
            pattern,
            urlconf_module,
            kwargs,
            app_name=app_name,
            namespace=namespace,
        )
    # callable判断
    elif callable(view):
        pattern = Pattern(route, name=name, is_endpoint=True)
        return URLPattern(pattern, view, kwargs, name)
    else:
        raise TypeError('view must be a callable or a list/tuple in the case of include().')

as_view()方法返回了一个内部定义的可调用函数:

@classonlymethod
def as_view(cls, **initkwargs):
    """Main entry point for a request-response process."""
    for key in initkwargs:
        if key in cls.http_method_names:
            raise TypeError(
                'The method name %s is not accepted as a keyword argument '
                'to %s().' % (key, cls.__name__)
            )
        if not hasattr(cls, key):
            raise TypeError("%s() received an invalid keyword %r. as_view "
                            "only accepts arguments that are already "
                            "attributes of the class." % (cls.__name__, key))

    # 内部定义了可调用函数
    def view(request, *args, **kwargs):
        self = cls(**initkwargs)
        self.setup(request, *args, **kwargs)
        if not hasattr(self, 'request'):
            raise AttributeError(
                "%s instance has no 'request' attribute. Did you override "
                "setup() and forget to call super()?" % cls.__name__
            )
        return self.dispatch(request, *args, **kwargs)
    view.view_class = cls
    view.view_initkwargs = initkwargs

    # take name and docstring from class
    update_wrapper(view, cls, updated=())

    # and possible attributes set by decorators
    # like csrf_exempt from dispatch
    update_wrapper(view, cls.dispatch, assigned=())
    return view

使用mixins

DRF提供了rest_framework.mixins模块,封装了类视图常用的增删改查方法:

比如新增CreateModelMixin

class CreateModelMixin:
    """
    Create a model instance.
    """
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.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 perform_create(self, serializer):
        serializer.save()

    def get_success_headers(self, data):
        try:
            return {'Location': str(data[api_settings.URL_FIELD_NAME])}
        except (TypeError, KeyError):
            return {}

类视图继承了Mixin后,可以直接使用它的.create()方法,类似的还有.list().retrieve().update().destroy()。我们按照这个思路来简化snippets/views.py代码:

from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import mixins
from rest_framework import generics

class SnippetList(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

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


class SnippetDetail(mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin,
                    generics.GenericAPIView):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

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

瞬间少了好多代码,真够DRY的。

什么是mixin?

维基百科的解释:

In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes.

不太好理解。

换句话说,mixin类提供了一些方法,我们不会直接用这些方法,而是把它添加到其他类来使用。

还是有点抽象。

再简单点说,mixin只不过是实现多重继承的一个技巧而已。

这下应该清楚了。

使用generics

如果仔细看snippets/views.py的代码,就会发现我们用到了from rest_framework import generics

generics.GenericAPIView

这是DRF提供的通用API类视图,mixins只提供了处理方法,views.py中的类要成为视图,还需要继承GenericAPIViewGenericAPIView继承了本文第一小节提到的rest_framework.views.APIView。除了GenericAPIView,我们还可以用其他的类视图进一步简化代码:

from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import generics


class SnippetList(generics.ListCreateAPIView):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer


class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer

看看ListCreateAPIView的源码:

class ListCreateAPIView(mixins.ListModelMixin,
                        mixins.CreateModelMixin,
                        GenericAPIView):
    """
    Concrete view for listing a queryset or creating a model instance.
    """
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

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

真DRY!

东方说

学到这里,已经开始感受到了Django REST framework的强大之处了,我觉得学一个框架,不仅要看如何使用,还需要了解它的设计思路和底层实现,这样才能更好的总结为自己的编程思想,写出更漂亮的代码。

参考资料: https://www.django-rest-framework.org/tutorial/3-class-based-views/#tutorial-3-class-based-views https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful https://www.zhihu.com/question/20778853

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 魔法方法推开Python进阶学习大门

    Python是Guido van Rossum设计出来的让使用者觉得如沐春风的一门编程语言。2020年11月12日,64岁的Python之父宣布由于退休生活太无...

    dongfanger
  • 接口自动化框架pyface详细介绍

    本框架系本人结合一些实践经验和开源框架设计思想,在家基于兴趣爱好独立完成的代码开发。

    dongfanger
  • java+appium+安卓模拟器实现app自动化Demo

    dongfanger
  • 学生宿舍管理系统(jsp+mysql).rar

    Java学生宿舍管理系统,技术架构方面使用jsp+mysql。源代码和数据库脚本齐全。有运行截图。适合大学生毕业设计参考和Java的初步学习者使用。

    用户4585225
  • Flink 自定义触发器实现带超时时间的 CountWindow

    Flink 的 window 有两个基本款,TimeWindow 和 CountWindow。 TimeWindow 是到时间就触发窗口,CountWindow...

    大数据技术与架构
  • IOS UITextField 选择出生日期

    // Presented when object becomes first responder.  If set to nil, reverts to fo...

    ZY_FlyWay
  • VVC编码进展:码率降低,速度仍需提升

    https://medium.com/@ewoutterhoeven/a-quick-and-dirty-look-at-vvc-16e8c2c56512 ...

    LiveVideoStack
  • 【CSS】872- 浅析rem布局方案

    这里说明一下,无论dpr多大,1px的大小通常来说是一致的,这也就意味着,随着dpr的增大,物理像素点会越来越小,这样才能容纳更多的物理像素,才能更高清,更re...

    pingan8787
  • 423. 有效的括号序列利用堆栈

    给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列。

    和蔼的zhxing
  • 怎么使用WordPress小工具添加新浪微博秀、一键关注等按钮

    怎么使用WordPress小工具添加新浪微博秀、一键关注等按钮?其实这个也不算什么稀奇了,就是直接使用新浪微博的微博组件

    沈唁

扫码关注云+社区

领取腾讯云代金券