Using the reserved word “class” as field name in Django and Django REST Framework

后端 未结 3 593
慢半拍i
慢半拍i 2020-12-06 11:37

Description of the problem

Taxonomy is the science of defining and naming groups of biological organisms on the basis of shared characteristics. Organisms are grou

3条回答
  •  一向
    一向 (楼主)
    2020-12-06 12:16

    You can rename field in the overloaded version of get_fields() method

    class MySerializer(serializers.Serializer):
        class_ = serializers.ReadOnlyField()
    
        def get_fields(self):
            result = super().get_fields()
            # Rename `class_` to `class`
            class_ = result.pop('class_')
            result['class'] = class_
            return result
    

提交回复
热议问题