文档建议我可以在基于类的视图中使用一个get,post,put方法。我该如何实现这样的东西?
class Test(APIView):
def post_modelA(request):
# code to create instances for model A
def post_modelB(request):
# code to create instances for model B
答案 0 :(得分:2)
可能会扩展GenericViewSet
并在urls.py
。
urlpatterns = [
path('test/modelA', views.Test.as_view({'post': 'post_modelA'})),
path('test/modelB', views.Test.as_view({'post': 'post_modelB'})),
]
答案 1 :(得分:0)
您可以编写两个基于类的视图
class TestA(APIView):
def post(self, request):
# code to create instances for model A
class TestB(APIView):
def post(self, request):
# code to create instances for model B