restframework中根據請求的類型修改序列化類

只要在視圖中重寫get_serializer_class方法就能夠,用if對請求的類型進行判斷spa

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

這個action的名字,若是是經過mixin定義的,就寫mixin的名字就行,如update   list  create 這樣,若是是經過action裝飾器本身定義的,就寫action的名字就能夠了code

 

get_serializer_class
相關文章
相關標籤/搜索