DRF之路由系统与视图

Posted fqh202

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DRF之路由系统与视图相关的知识,希望对你有一定的参考价值。

手动路由编写

# 路由
from django.conf.urls import url
from app01.views import UserListView


urlpatterns = [
    # GET请求 查询所有
    # POST请求 增加
    url(r'^users/$', UserListView.as_view()),
    url(r'^users.(?P<format>w+)/$', UserListView.as_view()),  # 附带.json后缀

    # PUT请求 修改
    # DELETE请求 删除
    # GET请求 查询单个数据
    url(r'^users/(?P<pk>d+)/$', UserListView.as_view()),
    url(r'^users.(?P<format>w+)/(?P<pk>d+)/$', UserListView.as_view()),
]



# 视图
from rest_framework import serializers
from .models import UserInfo
from rest_framework.response import Response

class MySerializer (serializers.ModelSerializer):

    class Meta:
        model = UserInfo  # 指定model
        fields = '__all__'  # 自动生成所有字段
        depth = 2  # 对于外键字段,可以显示


class UserListView(APIView):
    def get(self,request,*args,**kwargs):
        print(args)
        print(kwargs)
        # 区分查询 单个 或 所有数据
        pk = kwargs.get('pk','')
        if pk:
            obj = UserInfo.objects.filter(id=pk).first()
            ser = MySerializer(instance=obj, many=False)
        else:
            obj_list = UserInfo.objects.all()
            ser = MySerializer(instance=obj_list, many=True)
        return Response(ser.data)

    def post(self,request,*args,**kwargs):
        # 新增
        pass

    def put(self,request,*args,**kwargs):
        # 更新
        pass

    def delete(self,request,*args,**kwargs):
        # 删除
        pass

半自动路由与视图

# 路由

from django.conf.urls import url
from django.contrib import admin
from app01.views import UserViewSet


urlpatterns = [
    url(r'^admin/', admin.site.urls),

    #  以get方式提交,则调用list方法;以post方式提交,则调用create方法
    url(r'^test/$', UserViewSet.as_view({'get': 'list', 'post': 'create'})),
    
    url(r'^test/(?P<pk>d+)/$', UserViewSet.as_view(
        {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),
]


# 视图
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserInfo
        fields = "__all__"


class UserViewSet(ModelViewSet):
    queryset = UserInfo.objects.all()
    serializer_class = UserSerializer

视图继承关系图

技术分享图片

DRF提供了可以直接在页面进行 新增修改删除 等功能:

技术分享图片


全自动路由与视图

# 路由
from django.conf.urls import url, include
from rest_framework import routers


router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
]



# 视图
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserInfo
        fields = "__all__"


class UserViewSet(ModelViewSet):
    queryset = UserInfo.objects.all()
    serializer_class = UserSerializer

以上是关于DRF之路由系统与视图的主要内容,如果未能解决你的问题,请参考以下文章

DRF框架之路由Routers

DRF 之 路由组件

DRF视图家族

drf-视图集路由系统action装饰器

drf4 视图与路由组件

DRF之视图家族