tortoise / tortoise/tortoise-orm
Allow set default for fields in pydantic_model_creator
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Is your feature request related to a problem? Please describe.
In your example for FastAPI https://tortoise-orm.readthedocs.io/en/latest/examples/fastapi.html for user update you use a POST request. In my opinion it is more suitable for these type of queries to use PATCH requests, but for them to work the intended way we need all fields in a pydantic model created from pydantic_model_creator to have default None (like we have in a FastAPI cookiecutter project https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/schemas/user.py#L22), with this model on user update with
await Users.filter(id=user_id).update(**user.dict(exclude_unset=True))
we would be able to set only the fields that were provided in the request. As of now, I don't see any parameters in pydantic_model_creator function to create this type of model.
Describe the solution you'd like
We could add a boolean argument with a name like allow_none: if it is set to True on a model creation we add a default None to fields (except for the fields that already have a default value I guess). Or we could make it more customizable, like providing a dictionary with additional properties for fields or something like that.
Describe alternatives you've considered
An alternative would be just to have a pydantic model in my application with fields with a default None, but it would require to set rules for fields (like max_length for strings) also in this pydantic model, so if something changes we need to also make a change in the pydantic model. So it's less convenient.
Additional context
I could make a PR, but we need to find a proper solution, because right now I am not sure, how we should support this (if it is even should be supported).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the pydantic_model_creator entry point and the FastAPI example linked in the issue, then compare the cookiecutter user schema and the exclude_unset update pattern. Determine how generated fields should receive default None while preserving existing defaults, and verify that the resulting model supports PATCH-style partial updates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, python
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100