tortoise / tortoise/tortoise-orm

Ability to provide overriding queryset_name for PydanticMeta in submodel

Open
#520 0 comments 0 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

Is your feature request related to a problem? Please describe.
Currently, when I use tortoise.contrib.pydantic.pydantic_queryset_creator, I am able to pass through name, which allows me to override the name of my queryset for the final serialized JSON / dict. Issue here is that, if that queryset exists as a submodel, I am unable to pass a name through. Examples here are for Models of which the meta name (and model name) end in "y", meaning they are appended with an "s," vs. dropping the y and adding the more linguistically-correct (at least for english) "ies."

Describe the solution you'd like
For the PydanticMeta class, I would like to be able to provide an attribute that is checked in the pydantic_queryset_creator, if it is available, to use that name, otherwise default to the "add s at the end".

. . .

class Entity(Model):
    entity_id = fields.UUIDField(pk=True)
    name = fields.CharField(max_length=50, null=False, unique=True)
    
    class Meta:
        table = "entity"
        . . .

    class PydanticMeta:
        queryset_name = "entities"
        . . .
. . .

Describe alternatives you've considered
1. Change the algorithm from adding an "s," to adding "_list"
I had considered this as my initial approach, but one concern that comes up is this being a sneaky breaking change. This approach felt like bringing a sword to a surgery, and could affect many users.
2. Change algorithm to make the linguistic change from adding "s," to dropping "y" and adding "ies"
Thought about this as well, but I am not sure if it s the right approach, considering this adds complications to a relatively naive and simple process. That and it could open the door for users to want a multilingual approach to handling plurals.

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 with tortoise.contrib.pydantic.pydantic_queryset_creator and inspect how nested PydanticMeta settings determine queryset names. Add support for an optional PydanticMeta queryset_name while retaining the existing default, then run the relevant Pydantic serialization tests or add coverage showing the name is used for submodels.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, database
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.