tortoise / tortoise/tortoise-orm

How to exclude fields of nested objects that are in m2m relation when creating PydanticModel using pydantic_model_creator?

Open
#887 0 comments 1 reaction 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

Assuming following setup:

class Recipe(Model):
    ...
    ingredients = fields.ManyToManyField(model_name="models.Ingredient", related_name="recipes", null=True,
                                         on_delete=fields.SET_NULL,
                                         description="List of required ingredients")

class Ingredient(Model):
    name: fields.CharField()
    not_needed: fields.CharField()

I want to create a PydanticModel for Recipe that won't contain Ingredient.not_needed field, like so:
pydantic_model_creator(Recipe, exclude=("ingredients.not_needed"))
One thing that comes to my mind is to use computed property but are there any other approaches to this problem?

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 by reading pydantic_model_creator and the nested relation handling it uses. Check whether exclusions are supported across ManyToMany fields and whether existing tests cover nested model fields. Done means establishing and documenting a supported way to omit Ingredient.not_needed, or clearly scoping the required feature.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.