tortoise / tortoise/tortoise-orm
Add 'optional' argument to '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
While the pydantic_model_creator feature is useful, the exclude and include options are not as versatile as they need to be for the function to be as flexible as it likely should. Consider the following:
from tortoise import models, fields
class Model(models.Model):
uid: int
first_name: str
last_name: str
number: int
Not to get into specific of which framework, say we have the following routes:
GET /model/{uid}POST /model/filter
For the GET above, it's easy to see how
from tortoise.contrib.pydantic import pydantic_model_creator
from . import model
ModelIn = pydantic_model_creator(model.Model, include=('uid',))
Is useful and handy. However, consider the case where one may filter via first_name, last_name, or number. This would required a Pydantic schema that is:
from pydantic import BaseModel
class ModelFilter(BaseModel):
first_name: Optiona[str]
last_name: Optional[str]
number: Optional[int]
It would be handy if pydantic_model_creator took another argument, optional, to include such options. Therefore allowing us to do:
ModelFilter = pydantic_model_creator(model.Model, exclude=('uid',), optional=('first_name', 'last_name', 'number'))
This relates to #571 as well.
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 at the pydantic_model_creator entry point and trace how the include and exclude arguments determine generated fields. Check the related discussion in issue #571 if available. Done means an optional argument can make the named fields optional while preserving the existing include and exclude behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100