一、在有默认(create、list、retrieve、update、destroy)接口时自定义接口
class IterationsViewSet(MyModelViewSet):
queryset = uc_iterations.objects.all().order_by('id')
serializer_class = IterationsSerializer
pagination_class = DIYPagination # 配置自定义的分页器
filter_backends = (filters.SearchFilter, DjangoFilterBackend, filters.OrderingFilter,)
search_fields = ('iterations_name','personnel')
filterset_fields = ('project_id',)
authentication_classes = [MyAutheentication, ]
def getAllIterations(self, request, *args, **kwargs):
data = list(uc_iterations.objects.all().values("id","iterations_name"))
return APIRespones('1000','ok',True,data)
def destroy(self, request, *args, **kwargs):
id = kwargs.get('pk')
b = uc_checklist.objects.filter(iterations_id=id).values('id')
if b.exists():
return APIRespones('1000', '存在关联checklist数据,不可删除哟~', True)
else:
uc_iterations.objects.filter(pk=id).delete()
return APIRespones('1000', '删除成功,欧耶~', True)
路由注意事项:
路由末尾没有 "/" r'^iterations/list$',
router = DefaultRouter()
router.register('projects', caseviews.ProjectsViewSet)
router.register('iterations', caseviews.IterationsViewSet)
router.register('usecases', caseviews.UsecasesViewSet)
router.register('checklist',checklistviews.ChecklistViewSet)
router.register('report', reportviews.ReportViewSet)
router.register('reportcc', emailCc.EmailCcViewSet)
urlpatterns = [
path('', include(router.urls)),
url(r'^checkuri/$', checkUri.as_view({"post": "post"})),
# 迭代报告生成
url(r'^iterations/report/creat/$', IterationReportViewSet.as_view({"post": "post"})),
# 迭代报告发送邮件/查看邮件html
url(r'^iterations/report/handleemail/$', IterationReportViewSet.as_view({"get": "getemail","post":"sendemail"})),
#用例管理
url(r'^usecases/view_add$', UsecasesViewSet.as_view({"post":"view_add"})),
#查询全部指定的迭代信息(id,iteration_name)
url(r'^iterations/list$', caseviews.IterationsViewSet.as_view({"get":"getAllIterations"})),
]
urlpatterns += router.urls
二、在没有默认(create、list、retrieve、update、destroy)接口时自定义接口
class TapdproductView(APIView):
authentication_classes = [MyAutheentication, ]
#/api/tapd/product/?department_id=68&begintime=2022-11-03&endtime=2022-11-07
def get(self,request):
# 部门id
id=request.query_params.getlist("department_id",0)
#时间范围
be=request.query_params.get("begintime",0)
en=request.query_params.get("endtime",0)
return APIRespones(status=1000, data_msg=productout.pr_story_count(department_id=id, begintime=be, endtime=en))
路由注意事项:
路由末尾有 "/" url(r'^product/$', tapdviews.TapdproductView.as_view())
标签:自定义,get,接口,Django,iterations,router,post,id,view
From: https://www.cnblogs.com/chenxdnote/p/17846358.html