Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#30077 closed New feature (invalid)

TextField constructor needs a strip=False option

Reported by: Rob van der Linde Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In Django 1.9 an option strip=True was added to CharField, this is fine but it also seems to affect TextField and there is no way to pass a strip=False option to TextField constructor to turn that off.

The issue seems to be the formfield method, this has lead to people subclassing TextField to "fix" this addition to Django 1.9, for example:

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)

Other people fix it in the form and override the admin form, this has become a bit messy, it seems to be necessary to introduce a strip argument to the TextField constructor, to avoid having to create custom field types.

Change History (3)

comment:1 by Simon Charette, 5 years ago

Type: UncategorizedNew feature

Just to make it clear the strip=True option was added to forms.CharField and not db.CharField like the report seems to imply. Moving this option to the database layer has been discussed and rejected when the option was added to the form layer so I doubt there's consensus on adding an option to specialize TextField here given striping spaces should be the desired behaviour in most cases just like with db.CharField.

Other people fix it in the form and override the admin form

Can't this be simply done with formfield_overrides or formfield_for_dbfield?

comment:2 by Rob van der Linde, 5 years ago

Resolution: invalid
Status: newclosed

comment:3 by Rob van der Linde, 5 years ago

Ah I see, I'll have to find a Wagtail specific fix, though I must say I do find this feature added to Django 1.9 extremely annoying.

Note: See TracTickets for help on using tickets.
Back to Top