tomchristie/django-rest-framework — pull request #1422 by djm (2014-02-15 22:21:09+00:00)
Title: The default serializer field for NullBooleanField does not allow unsetting of the field back to NULL.

Hi all,

I came across an issue (or at least what I think is an issue) the other day and I thought I'd submit some lovely failing tests to get the conversation started on this.

Basically, when using a `ModelSerializer` the default serializer field used for Django's `NullBooleanField` is `rest_framework.fields.BooleanField` as can be [seen at the latest HEAD](https://github.com/tomchristie/django-rest-framework/blob/822eb39599b248c68573c3095639a831ab6df99a/rest_framework/serializers.py#L651).

This brings some unexpected behaviour with it when applied to nullable boolean fields because of DRF's `BooleanField.from_native` method, as can be seen on [this line](https://github.com/tomchristie/django-rest-framework/blob/822eb39599b248c68573c3095639a831ab6df99a/rest_framework/fields.py#L453), again from the latest head. Basically, it doesn't handle the `None` value case correctly; when the serializer field receives a `None` value (representing `null` from JSON say) it sets the return to `False` as `bool(None) == False`. `BooleanField`s themselves are not nullable but obviously NullBooleanField's are and therefore they should be allowed to be set back to NULL as this state may have meaning in a given application.

I've already implemented the fix in the project I'm currently working on; I simple override the `from_native` in a field subclass and then used that but I'm sure there are other things to think about which is why I'm starting this discussion. For example: it seems from historical issues #1183 & #1184 that the problem of the browseable API using the correct widget (Select) instead of a checkbox never got fixed.

I guess the first question is: do these issues justify a new `NullBooleanField` serializer field? I would probably be siding on the 'yes' front as dealing with None cases on a `BooleanField` is incorrect given their non-nullabillity (that's a word, sssh).

Anyway, I'm happy to do all the code/tests/docs work after this is a confirmed problem, so just let me know whenever you get a chance.

cheers!
Darian