Opened 9 years ago

Closed 8 years ago

#6937 closed (fixed)

newforms BooleanField should be more tolerant of different inputs

Reported by: Simon Willison Owned by: Andrew Gibson
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

Here's how BooleanField works at the moment:

http://code.djangoproject.com/browser/django/trunk/django/newforms/fields.py#L534

if value == 'False':
    return False
return bool(value)

It would be really nice if it had a slightly more human-friendly idea of what constituted true and false values. For example, '1' v.s. '0', 'true' v.s. 'false', 'yes' v.s. 'no' etc. At the moment it assumes it will only be called from a check box widget, but there are times when this code will be used for input that didn't come from an HTML form.

Attachments (1)

patch.diff (1.2 KB) - added by Andrew Gibson 8 years ago.
one line change plus tests

Download all attachments as: .zip

Change History (7)

comment:1 Changed 9 years ago by Chris Beaven

Triage Stage: UnreviewedDesign decision needed

Easy to implement, just need a call from core whether we want to do this...

if value.lower() in ('0', 'false', 'no'):
    return False
return bool(value)

comment:2 Changed 9 years ago by Chris Beaven

(of course, that was a naive prototype, you should check that value is a string too)

comment:3 Changed 8 years ago by Andrew Gibson

Owner: changed from nobody to Andrew Gibson
Status: newassigned

Changed 8 years ago by Andrew Gibson

Attachment: patch.diff added

one line change plus tests

comment:4 Changed 8 years ago by Andrew Gibson

Has patch: set

My first attempt at submitting a patch. I know it's not a big deal, was just trying to learn the process. Hope it helps:)

comment:5 Changed 8 years ago by Chris Beaven

Patch needs improvement: set

checking type against str isn't very good

comment:6 Changed 8 years ago by Chris Beaven

Resolution: fixed
Status: assignedclosed

This is actually a dupe ticket - it was fixed somewhere along the way (for '0' at least)

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