【发布时间】:2017-06-02 14:11:30
【问题描述】:
(我用的是最新的django/python/DRF)
我的 API 端点是应用程序内部的。它们与我在系统中的模型不匹配。我见过两种使用 API 视图的技术。
第一个使用方法,就像我正在使用的那样:
@api_view(['GET', 'POST'])
@authentication_classes([JSONWebTokenAuthentication])
def myApiEndPoint(request):
"""
This text is the description for this API.
"""
if request.method == 'GET':
return Response("ok get", status=status.HTTP_200_OK)
elif request.method == 'POST':
return Response("ok post", status=status.HTTP_200_OK)
第二个使用类定义,如 DRF 文档
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
第二种方式看起来更整洁有序。但是:
- 我可以将它用于非特定模型的验证吗?
- 如何在这种情况下定义每个类的权限和身份验证类?
谢谢
【问题讨论】: