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

[bug] computed feild is not loaded

Open kkv0id opened this issue 3 years ago • 1 comments

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

kkv0id avatar Feb 28 '22 11:02 kkv0id

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 中的字段就被忽略, 结果中不会出现

kkv0id avatar Mar 10 '22 06:03 kkv0id