如何在django-rest-framework中使用相同的URL conf调用

我正在尝试构建GET和POST方法来获取和保存一些对象。我有views.py这样


class QuestionList(generics.ListAPIView):

    queryset = Question.objects.all()

    serializer_class = QuestionSerializer


class QuestionSave(generics.CreateAPIView):

    queryset = Question.objects.all()

    serializer_class = QuestionSerializer

然后我有url conf,urls.py像这样


urlpatterns = [

    url(r'^questions/$',views.QuestionList.as_view())

    ]

以我的理解,我们必须有一个通用类,其POST方法为CreateApiView,方法为ListApiView GET,因此我创建了这样的类。我的问题是,我应该如何配置它们,以便在POSTQuestionSave上被调用而在GETQuestionList上被调用?


元芳怎么了
浏览 166回答 2
2回答

哈士奇WWW

使用ListCreateAPIView。它为视图提供get和post方法处理程序。参考:Django Rest Frameworkclass QuestionView(generics.ListCreateAPIView)     queryset = Question.objects.all()         serializer_class = QuestionSerializer

慕哥6287543

简单的!:class QuestionList(generics.ListAPIView, generics.CreateAPIView):     queryset = Question.objects.all()     serializer_class = QuestionSerializer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python