Skip to content

Add blank_nullable option to Model Schema #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tomatod
Copy link

@tomatod tomatod commented May 11, 2025

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.

  • An similar option only for string-based field. But, the issue occurs not only with string-based field and this implementation will be a little complicated, so I think the option for all kinds of field is better.
  • An change that can get to treat the Django blank option as required property of OpenAPI schema. I think the pattern of blank=True and null=False can be interpreted as "null cannot be used, but unspecifying is OK". Currently, nullable and required property of OpenAPI schema by django-ninja seem to be linked, meaning a field is required when a field is not nullable. But the behavior depends on Pydantic's base_model.model_json_schema() and changing it seems to be complicated.
  • An option to set default string to string-based fields. I think this is also limited and not appropriate.

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.

@tomatod tomatod force-pushed the add-blank-nullable-option branch from a06f573 to d90645f Compare May 11, 2025 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant