resfframework中修改序列化类的返回值

在序列化类中重写to_representation(self,instance)方法,这个是返回json对象的方法,返回的是一个待序列化的对象,能够直接对这个类进行定制,有关关联查询也能够在这里进行定制,例如git

    def to_representation(self, instance):
        res = super().to_representation(instance=instance)
        res["gitGroup"] = {"name": instance.gitGroup.name}
        res["project"] = {"name": instance.project.name}
        res["project2"] = instance.project.name
        return res
相关文章
相关标签/搜索