20

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/fields/#django.forms.CharField.strip

The same seams to apply to text fields TextField.

So I found out why Django suddenly behaves differently than before, but is there an easy way to restore the previous default for auto generated admin forms?

I would like to NOT strip spaces while still using the auto generated form from the admin. Is that still possible?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Andy
  • 726
  • 2
  • 6
  • 15

6 Answers6

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.

Shahrukh Mohammad
  • 985
  • 2
  • 17
  • 33
  • 1
    i already answered that question myself with a more compact syntax nearly a year ago :) – Andy Jun 20 '18 at 14:37
  • I am not sure how your solution will work in case I want the field from the model in the model form rather than defining a field, just to ensure it has all the validation checks defined in the model. Your 1-year-old solution didn't work for me so I came up with this solution and posted it, so it might help somebody. :) – Shahrukh Mohammad Jun 20 '18 at 16:47
  • Good point actually - reusing the generated field rather than redefining it, might save some work here in case the field is more complicated than my old usecase. – Andy Jun 21 '18 at 09:53
13

Try using this:

# fields.py
from django.db.models import TextField


class NonStrippingTextField(TextField):
    """A TextField that does not strip whitespace at the beginning/end of
    it's value.  Might be important for markup/code."""

    def formfield(self, **kwargs):
        kwargs['strip'] = False
        return super(NonStrippingTextField, self).formfield(**kwargs)

And in your model:

class MyModel(models.Model):
    # ...
    my_field = NonStrippingTextField()
Udi
  • 29,222
  • 9
  • 96
  • 129
  • 1
    Thats a nice idea .. will check that next time i run into this problem :) – Andy Nov 24 '16 at 09:50
  • This worked well to me! Couldn't find it so I posted [Is there a way to disable Django Admin removing trailing spaces in TextField strings?](https://stackoverflow.com/q/59500928/1983854) and was marked as a dupe of this. – fedorqui Dec 27 '19 at 14:55
7
strip=False 

in the model field for CharFields.


Django TextField do not support this stripping feature so you have to do it on your own. You can use the strip method.

abc.strip()
Aamish Baloch
  • 1,200
  • 12
  • 9
  • 5
    According to the docs the strip parameter is only valid for the form field but not for the model field .. and i of course tried: TypeError: __init__() got an unexpected keyword argument 'strip' – Andy Aug 17 '16 at 13:32
  • and like i said above i want to remove the strip feature which is the default starting with Django 1.9 - i basically like it, but for my current project i want to have spaces NOT stripped away – Andy Aug 17 '16 at 13:34
5

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!

Andy
  • 726
  • 2
  • 6
  • 15
3

You can also achieve this by modifying ModelAdmin.

Overridding formfield_for_dbfield function and setting kwargs['strip'] = False for title field will disable auto trim for it.

@admin.register(Example)
class ExampleAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if db_field.name == 'title':
            kwargs['strip'] = False
        return super().formfield_for_dbfield(db_field, request, **kwargs)

Ref: https://www.aaronoellis.com/articles/allow-whitespace-to-be-a-valid-charfield-value-in-django-admin

Hunger
  • 5,186
  • 5
  • 23
  • 29
2

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"]
Dap
  • 2,309
  • 5
  • 32
  • 44