tortoise-orm: Unable to use computed values from relations after Pydantic v2 upgrade

Describe the bug I’m unable to use computed values from relations since upgraded to Pydantic v2. When using the class method from_tortoise_orm the following error are occurring

  File "/some-path/projects/debug-tortoise-orm/.venv/lib/python3.11/site-packages/pydantic/main.py", line 353, in model_dump_json
    return self.__pydantic_serializer__.to_json(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: AttributeError: '__main__.Employee.leaf' object has no attribute 'team_members'

To Reproduce [CODE UPDATED] The following snippet shows how to reproduce.

from tortoise import Tortoise, fields, run_async
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.exceptions import NoValuesFetched
from tortoise.models import Model


class Employee(Model):
    name = fields.CharField(max_length=50)

    manager: fields.ForeignKeyNullableRelation["Employee"] = fields.ForeignKeyField(
        "models.Employee", related_name="team_members", null=True
    )
    team_members: fields.ReverseRelation["Employee"]

    talks_to: fields.ManyToManyRelation["Employee"] = fields.ManyToManyField(
        "models.Employee", related_name="gets_talked_to"
    )
    gets_talked_to: fields.ManyToManyRelation["Employee"]

    def name_length(self) -> int:
        return len(self.name)

    def team_size(self) -> int:
        try:
            return len(self.team_members)
        except NoValuesFetched:
            return -1


    class PydanticMeta:
        computed = ["name_length", "team_size"]
        allow_cycles = True
        max_recursion = 4


async def run():
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()

    Employee_Pydantic = pydantic_model_creator(Employee)

    root = await Employee.create(name="Root")
    loose = await Employee.create(name="Loose")
    _1 = await Employee.create(name="1. First H1", manager=root)
    _2 = await Employee.create(name="2. Second H1", manager=root)

    await _1.talks_to.add(_2, root, loose)
    await _2.gets_talked_to.add(_1, loose)

    # The error ocurs here, after calling `from_tortoise_orm`
    p = await Employee_Pydantic.from_tortoise_orm(await Employee.get(name="Root"))
    print(p.model_dump_json(indent=4))


if __name__ == "__main__":
    run_async(run())

Expected behavior Pydantic serializes the model.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (12 by maintainers)

Most upvoted comments

Since this issue has not yet been solved. I propose a temporary solution. Based on the solution from @waketzheng

monkey_patch.py

def pydantic_model_creator(
    cls: "Type[Model]",
    *,
    name=None,
    exclude: Tuple[str, ...] = (),
    include: Tuple[str, ...] = (),
    computed: Tuple[str, ...] = (),
    optional: Tuple[str, ...] = (),
    allow_cycles: Optional[bool] = None,
    sort_alphabetically: Optional[bool] = None,
    _stack: tuple = (),
    exclude_readonly: bool = False,
    meta_override: Optional[Type] = None,
    model_config: Optional[ConfigDict] = None,
    validators: Optional[Dict[str, Any]] = None,
    module: str = __name__,
) -> Type[PydanticModel]:
    pydantic_model = pmd(
        cls,
        name=name,
        exclude=exclude,
        include=include,
        computed=computed,
        optional=optional,
        allow_cycles=allow_cycles,
        sort_alphabetically=sort_alphabetically,
        exclude_readonly=exclude_readonly,
        meta_override=meta_override,
        model_config=model_config,
        validators=validators,
        module=module,
    )

    class MonkeyPatchedModel(pydantic_model):
        @classmethod
        async def from_tortoise_orm(cls, obj: "Model"):
            instance = await super().from_tortoise_orm(obj)
            setattr(instance, '_initial_obj', obj)
            return instance

    return MonkeyPatchedModel

base_model.py

def add_functionality_to_computed_method(method):
    @wraps(method)
    def _impl(self, *method_args, **method_kwargs):
        target = getattr(self, "_initial_obj", self)
        return method(target, *method_args, **method_kwargs)

    return _impl


class ComputedMethodsMeta(ModelMeta):
    def __new__(cls, name: str, bases: Tuple[Type, ...], attrs: dict):
        pydantic_meta = attrs.get("PydanticMeta")
        if pydantic_meta:
            computed_methods = getattr(pydantic_meta, "computed", [])
            for method_name in computed_methods:
                if method_name in attrs:
                    original_method = attrs[method_name]
                    attrs[method_name] = add_functionality_to_computed_method(
                        original_method
                    )
        return super().__new__(cls, name, bases, attrs)


class BaseModel(models.Model, metaclass=ComputedMethodsMeta):
    # fields

    class Meta:
        abstract = True


class Employe(BaseModel):
    other_entity: fields.OneToOneNullableRelation["Entity"] = OneToOneField(
        "models.Entity",
        related_name=False,
        null=True,
        on_delete=SET_NULL,
    )

    def entity_url(self) -> str | None:
        if getattr(self, "other_entity", None):
            return "some_url" + self.name
        return None

    class PydanticMeta:
        computed = ("entity_url",)

@janusheide Seems that it is not a bug, just use getattr instead of .attr

The following code snippet worked at my machine (MacOS+Python3.11+tortoise-orm0.20.1)

from __future__ import annotations

from tortoise import Tortoise, fields, run_async
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.fields import SET_NULL, CharField, IntField, OneToOneField
from tortoise.models import Model


class Entity(Model):
    id = IntField(pk=True)
    name = CharField(256)
    other_entity: fields.OneToOneNullableRelation[Entity] = OneToOneField(
        "models.Entity",
        related_name=False,
        null=True,
        on_delete=SET_NULL,
    )

    def entity_url(self: Entity) -> str | None:
        if getattr(self, "other_entity", None):
            return "some_url" + self.name
        return None

    class PydanticMeta:
        computed = ("entity_url",)


async def run() -> None:
    await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()

    Entity_Pydantic = pydantic_model_creator(Entity)
    root = await Entity.create(name="Root")
    print(f"{root.entity_url() = }")

    loose = await Entity.create(name="Loose", other_entity=root)
    print(f"{loose.entity_url() = }")

    p = await Entity_Pydantic.from_tortoise_orm(await Entity.get(name="Root"))
    print(p.model_dump_json(indent=4))

    p2 = await Entity_Pydantic.from_tortoise_orm(await Entity.get(name="Loose"))
    print(p2.model_dump_json(indent=4))


if __name__ == "__main__":
    run_async(run())