BooleanField- need to parse as false (not default value) when checkbox cleared in browsable API

1,129 views
Skip to first unread message

mat...@multapplied.net

unread,
Jan 22, 2013, 3:39:00 PM1/22/13
to django-res...@googlegroups.com
Hi,

I'd appreciate some input into the issue I'm having with django rest framework. I have a ModelSerializer for a model with a BooleanField that defaults to true. In the browsable API, this field, which is represented by an HTML checkbox, cannot be set to false.

The effect is like this:
  • Suppose in the browsable API there is a form for creating a new object. The form has a checkbox for the model field which defaults to true.
  • As the user completes the form for the new object, he decides to set the field to false. He clears the checkbox.
  • When submitting the form, the browser completely omits the data for the checkbox, since it was cleared.
  • Django rest framework handles the form and sees that the checkbox data is missing. Instead of deciding that the field on the new object should be set to false, it uses the default value and sets it to true.
This happens when updating models, not just creating new ones. Due to this problem, the user cannot set a boolean field to false when the default value is true, when using the browsable API.

A workaround is to duplicate the field in the serializer and explicitly set it to default false, like so:
compression = serializers.BooleanField(source='compression', default=False)

But that doesn't seem very DRY, and it also makes the browsable API a little harder to use because the checkboxes that should be checked by default on the model creation forms are all now unchecked.

There is a comment in BooleanField in fields.py that seems to indicate someone has thought about this:
class BooleanField(WritableField):
    ..........
    # Note: we set default to `False` in order to fill in missing value not
    # supplied by html form.  TODO: Fix so that only html form input gets
    # this behavior.
    default = False

But at least on my system this doesn't fix the issue.

So my questions are:
  • Is this issue handled properly in django rest framework, and something in my app has broken it?
  • If not, is there a better workaround than the one I'm already doing?

Thanks in advance,
Matt Fox

Jani Tiainen

unread,
Jan 23, 2013, 5:36:45 AM1/23/13
to django-res...@googlegroups.com
Hi,

To me it seems that you might have found a bug!

Since for a boolean field there exists only two possible values: true or
false. And as for checkbox "true" means it's checked and "false" is
uncheked.

If behaviour is as you described and unchecked checkbox will default to
default value (which might be true) it's working incorrectly and should
be fixed.

Probably Tom would say: patches welcome. (or pull requests)

22.1.2013 22:39, mat...@multapplied.net kirjoitti:
> Hi,
>
> I'd appreciate some input into the issue I'm having with django rest
> framework. I have a ModelSerializer for a model with a BooleanField that
> defaults to true. In the browsable API, this field, which is represented
> by an HTML checkbox, cannot be set to false.
>
> The effect is like this:
>
> * Suppose in the browsable API there is a form for creating a new
> object. The form has a checkbox for the model field which defaults
> to true.
> * As the user completes the form for the new object, he decides to set
> the field to false. He clears the checkbox.
> * When submitting the form, the browser completely omits the data for
> the checkbox, since it was cleared.
> * Django rest framework handles the form and sees that the checkbox
> data is missing. Instead of deciding that the field on the new
> object should be set to false, it uses the default value and sets it
> to true.
>
> This happens when updating models, not just creating new ones. Due to
> this problem, the user cannot set a boolean field to false when the
> default value is true, when using the browsable API.
>
> A workaround is to duplicate the field in the serializer and explicitly
> set it to default false, like so:
> compression = serializers.BooleanField(source='compression', default=False)
>
> But that doesn't seem very DRY, and it also makes the browsable API a
> little harder to use because the checkboxes that should be checked by
> default on the model creation forms are all now unchecked.
>
> There is a comment in BooleanField in fields.py that seems to indicate
> someone has thought about this:
> class BooleanField(WritableField):
> ..........
> # Note: we set default to `False` in order to fill in missing value not
> # supplied by html form. TODO: Fix so that only html form input gets
> # this behavior.
> default = False
>
> But at least on my system this doesn't fix the issue.
>
> So my questions are:
>
> * Is this issue handled properly in django rest framework, and
> something in my app has broken it?
> * If not, is there a better workaround than the one I'm already doing?
>
> Thanks in advance,
> Matt Fox
>


--
Jani Tiainen

- Well planned is half done and a half done has been sufficient before...

Tom Christie

unread,
Jan 24, 2013, 7:53:41 AM1/24/13
to django-res...@googlegroups.com
Hi Matt,

  As Jani says, that sounds like a valid bug.  Please do go ahead an file a ticket for it.  Pull requests are always welcome of course, even if it's just a failing test that demonstrates the issue.

Cheers,

  Tom

Colleen Briant

unread,
Jan 14, 2016, 7:49:15 PM1/14/16
to Django REST framework
Has this been fixed? I'm still seeing it.
Reply all
Reply to author
Forward
0 new messages