Add blank_nullable option to Model Schema #1460
Open
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.
Summary
Added the blank_nullable option to ModelSchema. The option specifies whether the original Django field's blank option is used to decide nullability.
This is related to the issue #1446. I withdrew the similar PR #1447 once but I thought it was appropriate, so I make this PR again.
By default, this option is True and doesn't change the current behavior considering the compatibility.
Background
Currently, the ModelSchema field generated from a Django field with blank=True is treated as nullable. In this case, the ModelSchema field accept null but Django Model field with null=False may not accept it.
This issue primarily occurs with string-based fields because they have the pattern of blank=True and null=False as one of the general patterns. The pattern is mentioned at null and blank sections in Django Model field Reference.
The null option of Django field is purely database-related according to the above reference. JSON can explicitly express null unlike the action with application/x-www-form-urlencoded content-type. I think as an option it would be good if ModelSchema could simply look only at the original Django field's null option because ModelSchema is literally generated from Model.
Other implementation
I thought of other implementations as bellow.
About DRF
In Django Rest Framework, ModelSerializer convert blank and null option on a field of its original Django Model to allow_blank which decides to allow "" or not and allow_null which decides to allow null on its Serializer field.
So I think the users migrating their projects from DRF may also set blank=True and null=False to Django Model and have the same issue.