tortoise / tortoise/tortoise-orm

Add 'optional' argument to 'pydantic_model_creator'.

Open
#642 1 comment 4 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.