tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

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

Open smomop opened this issue 10 months ago • 4 comments

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.

smomop avatar Aug 21 '23 08:08 smomop