restframework中根据请求的类型修改序列化类

只要在视图中重写get_serializer_class方法就可以,用if对请求的类型进行判断

    def get_serializer_class(self):
        if self.action == "update":
            return GitProjectUpdateSerializer
        return GitProjectSerializer

这个action的名字,如果是通过mixin定义的,就写mixin的名字就行,如update   list  create 这样,如果是通过action装饰器自己定义的,就写action的名字就可以了

get_serializer_class

猜你喜欢

转载自www.cnblogs.com/wuxie1989/p/10876978.html
今日推荐