Repository navigation
Don't propagate model BooleanField defaults to serializer fields - #10115
Open
LindseyZ1205 wants to merge 1 commit into
Open
LindseyZ1205 wants to merge 1 commit into
LindseyZ1205 wants to merge 1 commit 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.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.