tortoise / tortoise/tortoise-orm

How to map M2M manually in pydantic class?

Open
#891 4 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

Following setup:

class Person(Model):
   ... 
   items = fields.ManyToManyField('models.Item', related_name="people")

class ItemDTO(BaseModel):
   ...

class PersonDTO(BaseModel):
   ...
   items: list[ItemDTO] #this does not work
   class Config:
       orm_mode=True

When trying:

PersonDTO.from_orm(await Person.get_or_none(pk=1))

It gives me an error:

items value is not a list

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 the Person, Item, and DTO definitions and the PersonDTO.from_orm call shown in the issue. Reproduce the “items value is not a list” error with the ManyToMany relation, then determine whether the expected outcome is implementation guidance or a library/documentation change; the report does not define what done looks like.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, database
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.