我正在尝试构建GET和POST方法来获取和保存一些对象。我有views.py这样class QuestionList(generics.ListAPIView): queryset = Question.objects.all() serializer_class = QuestionSerializerclass 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上被调用?
2 回答

哈士奇WWW
TA贡献1799条经验 获得超6个赞
使用ListCreateAPIView。它为视图提供get和post方法处理程序。
class QuestionView(generics.ListCreateAPIView) queryset = Question.objects.all() serializer_class = QuestionSerializer

慕哥6287543
TA贡献1831条经验 获得超10个赞
简单的!:
class QuestionList(generics.ListAPIView, generics.CreateAPIView): queryset = Question.objects.all() serializer_class = QuestionSerializer
添加回答
举报
0/150
提交
取消