Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除django rest序列化程序上的额外空白_Django_Django Rest Framework_Django Serializer - Fatal编程技术网

如何删除django rest序列化程序上的额外空白

如何删除django rest序列化程序上的额外空白,django,django-rest-framework,django-serializer,Django,Django Rest Framework,Django Serializer,我有一个问题,你在django中序列化一个对象,它看起来是这样的 {"title": " remove all spaces "} 在序列化程序中,可以设置额外的_kwargs来修剪字段。结果是下一个 {"title": "remove all spaces"} 有没有办法删除“删除”和“全部”之间多余的空格 以下是序列化程序示例: class exampleSerializer(serialize

我有一个问题,你在django中序列化一个对象,它看起来是这样的

{"title": "   remove   all spaces  "}
在序列化程序中,可以设置额外的_kwargs来修剪字段。结果是下一个

{"title": "remove   all spaces"}
有没有办法删除“删除”和“全部”之间多余的空格

以下是序列化程序示例:

class exampleSerializer(serializers.ModelSerializer):
  
  class Meta:
     model = Example
     fields = ("title", )
     extra_kwargs = {"content": {"trim_whitespace": True}}


将to_表示方法添加到序列化程序:

    def to_representation(self, data):
        data = super(exampleSerializer, self).to_representation(data)
        content = data['content']
        data['content'] = " ".join(content.split())
        return data

DRF没有此功能。但是,您可以使用SerializerMethodField。请问您是想删除
保存数据
还是
显示数据
中的空格?