3

I am using a Textarea in Django admin with strip=False as outlined here.

def formfield_for_dbfield(self, db_field, **kwargs):
    formfield = super().formfield_for_dbfield(db_field, **kwargs)
    if db_field.name == 'some_text':
        formfield.strip = False
        formfield.widget = forms.Textarea(attrs={'rows': 10, 'cols': 80})
    return formfield

The DB field comes from an HTML input box, and has newlines in it (\n).

After I edit and save in Django, it has carriage-return newlines (\r\n), as seen when I query from the DB.

BEFORE:

select md5(some_text), some_text from myapp_obj where id = 328;
               md5                |                         some_text                          
----------------------------------+-----------------------------------------------------------
 adb48a782562ef02801518c4e94ca830 | foo1.                                             +
                                  | foo2                                              +
...   

AFTER:

select md5(some_text), some_text from myapp_obj where id = 328;
               md5                |                         some_text                          
----------------------------------+-----------------------------------------------------------
 e9e06c1d9764be70fc61f05c5fd6292c | foo1.\r                                             +
                                  | foo2\r                                              +
...   

How do I get the Django admin UI (the Textarea) to save back exactly what was loaded into its UI, without adding or subtracting anything?

I'm using Django 3.2.9, Postgres 13.

dfrankow
  • 20,191
  • 41
  • 152
  • 214

0 Answers0