1👍
✅
This can be accomplished by overwriting the to_python method on the form Field. This runs as part of serializing the form data into python, so it happens before any field validation or cleaning (which could otherwise error due to incorrect line lengths). Note that this is different from overwriting the widget (which controls how the form displays in HTML) or the model field (which controls how the data is stored in the database).
from django.utils.text import normalize_newlines
class MyCharField(forms.CharField):
def to_python(self, value):
# NOTE: No security guarantees are made about this code
return super().to_python(normalize_newlines(value))
And then overwriting the field used in the form like so:
class MyForm(forms.ModelForm):
class Meta:
widgets = {
"text": forms.Textarea() # Unchanged
}
field_classes = {
"text": MyCharField,
}
Source:stackexchange.com