Django TextField and CharField is stripping spaces and blank lines

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

    Seems like the best way to handle this is to create a custom admin form like this:

    class CustomForm(forms.ModelForm):
        my_field = forms.CharField(strip=False, widget=forms.Textarea)
    
        class Meta:
            model = MyModel
            exclude = []
    

    This will create a default form with just my_field overwritten with its non stripped version. )this has to be set in the corresponding admin of course. If anybody knows an even simpler version. Please tell me!

提交回复
热议问题