Add strict_null option for ModelSchema to decide nullability of a field from Django field's null option #1520
+120
−3
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
Thank you for this great API framework. It's very useful to develop Django applications with obvious interfaces between frontend and backend.
This PR is for an issue #1446 I raised about Schema's fields accepting null if their model field's blank option is True.
When this option is set to True, ModelSchema simply decides an field's nullability according to the Django model field's null option. I think this simple option is useful for many users considering the number of thumb up in the #1446 (comment)
If you have some good ideas, advices or naming, I'll be happy to know it and improve my PR (even other's PR).
Also, similarly previous PR #1460 will be closed because I think this PR is better than it (sorry for making similar PR, but I think resolving this issue is useful. Also, this PR will be my last idea).
What I considered
What I'm facing on (as an example)
I'll share one of the issues I'm facing on due to Django field's blank=True considered as allowing null on Schema. I think some other user may have same issues.
In case of creating or updating, ModelSchema is looser to null than its Model, so an issue 'Schema accepts null but Model denies null' is frequently raised without client considering the background between the schema and the model. If the client side generate API client from the OpenAPI schema automatically by some libraries, the client may sets null to the field and call the API because literally the field defined as accepting null.
For these issues, we can override the automatically generated fields by ModelSchema, but it takes not a few cost and sometimes makes mistakes (typos etc.). Also, we can fundamentally change the Django's model field's options but I think it's not easy.
Thanks,