✨ Add model_fields_optional to convert inherited fields to Optional#1830
Open
Krishnachaitanyakc wants to merge 3 commits intofastapi:mainfrom
Open
✨ Add model_fields_optional to convert inherited fields to Optional#1830Krishnachaitanyakc wants to merge 3 commits intofastapi:mainfrom
model_fields_optional to convert inherited fields to Optional#1830Krishnachaitanyakc wants to merge 3 commits intofastapi:mainfrom
Conversation
Add support for `model_fields_optional="all"` configuration that makes all inherited fields Optional with a default of None. This enables the common pattern of creating "update" models where all fields are optional, reducing boilerplate when building CRUD APIs. Fixes fastapi#64
…nting - Use `copy.copy()` fallback for FieldInfo when `_copy()` is unavailable (added in pydantic 2.12.0, but project supports >=2.11.0) - Replace `Union[X, None]` with `X | None` to satisfy ruff UP007/UP045 - Apply ruff formatting fixes
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.
Summary
Adds a new
model_fields_optionalconfiguration option that makes all inherited fieldsOptionalwith a default ofNonewhen set to"all". This enables the common pattern of creating "update" models where all fields are optional, reducing boilerplate when building CRUD APIs with SQLModel.Before (manual boilerplate):
After (with
model_fields_optional):Key behaviors
Optional[X] = NoneOptionalkeep their existing defaultsmin_length,ge) are preserved and still validated for non-None valuesmodel_fields_optional="all") or viamodel_configmodel_dump(exclude_unset=True)correctly returns only the fields that were explicitly settable=Truebase models (the update model itself is not a table)Changes
sqlmodel/_compat.py: Addedmodel_fields_optionaltoSQLModelConfigsqlmodel/main.py: Added_is_optional_annotation()helper and processing logic inSQLModelMetaclass.__new__tests/test_model_fields_optional.py: 11 test cases covering basic usage, partial data, exclude_unset, field override, constraint preservation, multiple inheritance, model_config style, table base compatibility, already-optional fields, model_validate, and JSON schemaFixes #64
Test plan
Field(min_length=...),Field(ge=...)constraints