• 类视图(APIView)


    from rest_framework.views import APIView
    from meituan.models import Merchant
    from django.http import Http404
    from .serializers import MerchantSerializer
    from rest_framework.response import Response
    from rest_framework import status
    
    ###################### APIView的代码 ######################
    class MerchantView(APIView):
        """
        检索, 更新和删除一个merchant实例对象.
        """
        def get_object(self, pk):
            try:
                return Merchant.objects.get(pk=pk)
            except Merchant.DoesNotExist:
                raise Http404
    
        def get(self, request, pk=None):
            if pk:
                merchant= self.get_object(pk)
                serializer = MerchantSerializer(merchant)
                return Response(serializer.data)
            else:
                queryset = Merchant.objects.all()
                serializer = MerchantSerializer(instance=queryset,many=True)
                return Response(serializer.data)
    
        def put(self, request, pk):
            merchant = self.get_object(pk)
            serializer = MerchantSerializer(merchant, 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):
            merchant= self.get_object(pk)
            merchant.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
    
    from django.urls import path
    from .views import MerchantView
    
    app_name = 'classview'
    urlpatterns = [
        # 列表:/merchant/ get
        # 新增:/merchant/ post
        # 详情:/merchant/[pk]/ get
        # 修改:/merchant/[pk]/ put
        # 删除:/merchant/[pk]/ delete
        path('merchant/',MerchantView.as_view()),
        path('merchant/<int:pk>/',MerchantView.as_view()),
    ]
    
  • 相关阅读:
    windows下大数据开发环境搭建(3)——Scala环境搭建
    windows下大数据开发环境搭建(1)——Java环境搭建
    windows下大数据开发环境搭建(2)——Hadoop环境搭建
    I/O复用
    SuRF : Practical Range Query Filtering with Fast Succinct Tries
    信号处理
    进程间通信
    简易内存分配器的实现
    socket编程(C++)
    C++—程序的内存分区
  • 原文地址:https://www.cnblogs.com/xshan/p/14193392.html
Copyright © 2020-2023  润新知