Jelajahi Sumber

学习使用一种新的,靠序列号来实现api

DYaiu 4 tahun lalu
induk
melakukan
ad1989c1df
3 mengubah file dengan 24 tambahan dan 2 penghapusan
  1. 10 0
      workreport/serializer.py
  2. 7 2
      workreport/urls.py
  3. 7 0
      workreport/views.py

+ 10 - 0
workreport/serializer.py

@@ -0,0 +1,10 @@
+from rest_framework import serializers
+
+from workreport.models import DailyReport
+
+
+class DailyReportSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = DailyReport
+        # 全部都包含的意思
+        fields = '__all__'

+ 7 - 2
workreport/urls.py

@@ -1,7 +1,12 @@
 from django.conf.urls import url
-from workreport.views import DailyReportViews
+from rest_framework_bulk.routes import BulkRouter
 
-urlpatterns = []
+from workreport.views import DailyReportViews, ReportViews
+
+router = BulkRouter()
+router.register(r'report', ReportViews)
+
+urlpatterns = router.urls
 
 urlpatterns += [
     url(r'^dailyreport/', DailyReportViews.as_view())

+ 7 - 0
workreport/views.py

@@ -4,8 +4,10 @@ from django.shortcuts import render
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
+from rest_framework.viewsets import ModelViewSet
 
 from workreport.models import DailyReport
+from workreport.serializer import DailyReportSerializer
 
 
 class DailyReportViews(APIView):
@@ -18,3 +20,8 @@ class DailyReportViews(APIView):
         report = DailyReport(title=data['title'], content=data['content'], create_by_id=data['create_by_id'])
         report.save()
         return Response(status=status.HTTP_201_CREATED)
+
+
+class ReportViews(ModelViewSet):
+    queryset = DailyReport.objects.all()
+    serializer_class = DailyReportSerializer