tortoise / tortoise/tortoise-orm

Features similar to django drf source field, I don't know how to implement it

Open
#1,456 4 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

class House(Model):
	id = fields.IntField(pk=True)
	house_name: str = fields.CharField(max_length=255)
	info: str = fields.CharField(max_length=255)
	user = fields.ForeignKeyField(
        "models.User",
        on_delete=fields.SET_NULL,
        related_name="houses",
        null=True,
    )

class User(Model):
	id = fields.IntField(pk=True)
	name: str = fields.CharField(max_length=255)
	houses: fields.ReverseRelation["House"]
When I was in get User, like django's drf, 
how to set the user foreign key field in the House model to a source field field, 
so that it can get the name data directly from the User model?

Raw data:
{
    "name": "test",
    "houses": [
        {
            "house_name": "one",
            "info": "xxxxx"
        },
        {
            "house_name": "two",
            "info": "xxxxx"
        }
    ]
}
Expected data:
{
    "name": "test",
    "houses": [
        {
            "house_name": "one",
            "info": "xxxxx",
            "user": "test"
        },
        {
            "house_name": "two",
            "info": "xxxxx",
            "user": "test"
        }
    ]
}

I am pydantic beginner, I don't know how to use pydantic to define the data I want to get.

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

Begin with the House/User relationship definitions and the Pydantic serialization approach shown in the issue. Check whether the requested nested output is supported by existing relation serialization; done would be a documented or implemented way to expose User.name in each House.user value. No repository files or tests are named, so the relevant entry points still need to be located.

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
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.