tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.62k stars 384 forks source link

[bug] computed feild is not loaded #1078

Open kexirong opened 2 years ago

kexirong commented 2 years ago

If any relational fields are added to exclude args of pydantic, the computed field will be ignored and not loaded

kexirong commented 2 years ago
class Tournament(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()

    events: fields.ReverseRelation["Event"]

    def __str__(self):
        return self.name

class Event(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()
    tournament: fields.ForeignKeyRelation[Tournament] = fields.ForeignKeyField(
        "models.Tournament", related_name="events"
    )
    participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField(
        "models.Team", related_name="events", through="event_team"
    )
    created = fields.DatetimeField(auto_now_add=True)
    updated = fields.DatetimeField(auto_now=True, null=True)

    def created_at(self) -> int:
        return int(self.created.timestamp())

    def updated_at(self) -> int:
        if self.updated is None:
            return 0
        return int(self.updated.timestamp())

    class PydanticMeta:
        exclude = ("created", "updated","participants" )
        computed = ("created_at", "updated_at")

    def __str__(self):
        return self.name

上面的模型,添加了关系字段participantsexclude中, computed中的字段就被忽略, 结果中不会出现