Repository navigation
Don't propagate model BooleanField defaults to serializer fields - #10115
LindseyZ1205 wants to merge 4 commits into
Conversation
Since encode#9997, the `ModelSerializer` field for a `BooleanField(default=True)` gets `default=True`. A `BooleanField` with a default reads a value missing from HTML form input as that default, and HTML forms leave out unchecked checkboxes, so a form could no longer set the field to `False`: an unchecked box was saved as `True`. With a callable default, the form failed with "Must be a valid boolean.", and a `null=True` field got its default instead of `None`. Skip the default for boolean model fields, as for relational ones, so these fields behave as they did before encode#9997.
browniebroke
left a comment
There was a problem hiding this comment.
Thanks for the fix, but I think we need a bit more test coverage as I'm not convinced we fixed all the use cases:
- Declaring a
BooleanField(default=True)on a plainSerializer - Custom model field mapped to
serializers.BooleanField - Full JSON PUT omitting the boolean field
| The `default` is not applied during partial update operations. In the partial update case only fields that are provided in the incoming data will have a validated value returned. | ||
|
|
||
| If you're using [Model Serializer](https://www.django-rest-framework.org/api-guide/serializers/#modelserializer) and the corresponding `Model` field declares a `default`, it will be used as the serializer field default. This does not apply to relational fields such as `ForeignKey`: a model default on a relational field is expressed as a primary key value rather than a model instance, so it is not propagated and only causes the generated serializer field to be not required. | ||
| If you're using [Model Serializer](https://www.django-rest-framework.org/api-guide/serializers/#modelserializer) and the corresponding `Model` field declares a `default`, it will be used as the serializer field default. This does not apply to relational fields such as `ForeignKey`: a model default on a relational field is expressed as a primary key value rather than a model instance, so it is not propagated and only causes the generated serializer field to be not required. Nor does it apply to `BooleanField`: HTML forms leave out unchecked checkboxes, so with the model default, a form could not set the field to `False`. |
There was a problem hiding this comment.
I find that the whole paragraph reads a bit weird, I think it's mainly down to the construct "This does not apply to... [long sentence]. Nor..."
How about:
This does not apply to
BooleanFieldor relational fields such asForeignKey. HTML forms leave out unchecked checkboxes, so with theBooleanFieldmodel default, a form could not set the field toFalse. A model default on a relational field is expressed as a primary key value rather than a model instance, so it is not propagated and only causes the generated serializer field to be not required
Not sure if my suggestion is the best way to phrase it either...
|
@M2004GV it would be great if you try and review this as well |
Check the serializer field class in `build_standard_field()`, instead of the model field class in `get_field_kwargs()`, so that a custom model field mapped to `BooleanField` doesn't get the model default either. Also test that a `BooleanField(default=True)` declared on a plain `Serializer` still uses its default for a missing HTML value, and that a full JSON update leaving a boolean field out keeps its value. Reword the docs as suggested in the review.
|
Hi, I tested the same scenarios through an actual The latest commit addresses the cases raised in the review: the HTML checkbox case is fixed, and the full test suite passes on this branch in the two environments I tested (Python 3.12/Django 6.1 and Python 3.10/Django 5.2). However, the problem is wider than On current main, a full Before #9997, these values were preserved. With this PR, omitted booleans are preserved again, but the other field types are still overwritten. I also found an important historical precedent that I should have caught before submitting #9997:
The current behavior appears to reintroduce the same regression. On this branch, both expectations now sit side by side in Before proposing a fix, could the maintainers clarify the intended contract: should model defaults be applied when a field is omitted during a full update? Separately, I noticed that the |
Description
refs #9997
Since #9997,
ModelSerializergives the field for a modelBooleanField(default=True)adefault=True. ABooleanFieldwith a default reads a value that is missing from HTML form input as that default, and an HTML form leaves out an unchecked checkbox. So an HTML form can no longer set such a field toFalse:Through a
ModelViewSet, a multipartPUTwith the box unchecked leaves the stored valueTrue. With a callable default, the same form fails with "Must be a valid boolean.", and aBooleanField(null=True, default=False)getsFalseinstead ofNone.The
BooleanFielddocs also say that an omitted value in HTML form input counts asFalse, "even if it has adefault=Trueoption specified".This skips the model default for boolean fields, as for relational fields, so they behave as they did before #9997. JSON input that leaves the field out still gets the model default when the instance is saved. Serializer
BooleanFields declared with an explicitdefaultare unchanged.The new test fails on main and passes with this change. The full test suite passes on Django 6.1.2 and Django main.