Skip to content

Conversation

tomatod
Copy link

@tomatod tomatod commented Aug 3, 2025

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

  • For compatibility, the option is set to False by default and will not change current behavior without explicitly setting it to True.
  • If a ModelSchema's field is defined with Optional type hint, It's also considered as allowing null.
  • Primary key isn't considered as allowing null by default for the following reason.
    • In case of describing (GET), id (meaning primary key) isn't probably be null.
    • In case of creating or updating, I think id will not be set in many patterns.

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,

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