Django TextField and CharField is stripping spaces and blank lines

后端 未结 5 1306
[愿得一人]
[愿得一人] 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:17

    If you are looking for a text/char field and do not want it to strip white spaces you can set strip=False in the constructor method of a form and then use the form in the admin

    class YourForm(forms.ModelForm):
    
        def __init__(self, *args, **kwargs):
            super(YourForm, self).__init__(*args, **kwargs)
            self.fields['myfield'].strip = False
    
        class Meta:
            model = YourModel
            fields = "__all__"
    

    You can then use this form in the admin by specifying form=YourForm in the admin.py file.

提交回复
热议问题