Django TextField and CharField is stripping spaces and blank lines

后端 未结 5 1305
[愿得一人]
[愿得一人] 2021-01-07 23:39

I just researched my \"bug\" and it turned out to be a new feature in Django 1.9 that CharFields strip spaces by default : https://docs.djangoproject.com/en/1.9/ref/forms/fi

5条回答
  •  悲&欢浪女
    2021-01-08 00:28

    I was having this issue with django-rest model serializer. The data in my text field was stripped of white space. So if you are trying to do this on the serializer level, you can specify the whitespace param on CharField serializer. Here is the source code signature.

    And here is the rest-docs on CharField

    class SomeSerializer(serializers.ModelSerializer):
        content = serializers.CharField(trim_whitespace=False)
    
        class Meta:
            model = YourModel
            fields = ["content"]
    

提交回复
热议问题