#Am I using DTO correctly?

1 messages · Page 1 of 1 (latest)

cedar pike
#

Hi, I looked at the documentation and fullstack as my reference for creating a DTO. Unfortunately, I am encountering an error when returning the Employee which is a child model of the UserModel

#DTO.py
from typing import Annotated

from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTO
from litestar.dto import DTOConfig

from app.core.models.employee import EmployeeModel

config = DTOConfig(
    exclude={
        "user_account.id",
        "user_account.created_at",
        "user_account.updated_at",
        "user_account.hashed_password",
    }
)

ReadEmployeeDTO = SQLAlchemyDTO[Annotated[EmployeeModel, config]]
#employee.py
from uuid import UUID

from sqlalchemy.orm import Mapped, relationship, mapped_column
from sqlalchemy import ForeignKey, Enum

from app.core.models import PermissionModel, CustomerModel
from app.core.models.users import UserModel, UserType

class EmployeeModel(UserModel):
    __tablename__ = "employees_account"

    id: Mapped[UUID] = mapped_column(ForeignKey("users_account.id"), primary_key=True)
    is_admin: Mapped[bool] = mapped_column(nullable=False, default=False)
    department: Mapped[Department] = mapped_column(Enum(Department), nullable=False)
    customer: Mapped[list["CustomerModel"]] = relationship(
        "CustomerModel",
        back_populates="representative",
        foreign_keys=[CustomerModel.id],
    )
    permission: Mapped[list["PermissionModel"]] = relationship(
        "PermissionModel", back_populates="user", foreign_keys=[PermissionModel.user_id]
    )

    __mapper_args__ = {
        "polymorphic_identity": UserType.employee,
    }
karmic sierraBOT
#
Notes for Am I using DTO correctly?
At your assistance

@cedar pike

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

cedar pike
#
#user.py
from sqlalchemy.orm import relationship, Mapped, mapped_column
from litestar.contrib.sqlalchemy.base import UUIDAuditBase

class UserModel(UUIDAuditBase):
    __tablename__ = "users_account"

    first_name: Mapped[str] = mapped_column(nullable=False)
    last_name: Mapped[str] = mapped_column(nullable=False)
    email: Mapped[str] = mapped_column(nullable=False, unique=True)
    phone: Mapped[str] = mapped_column(nullable=False, unique=True)
    hashed_password: Mapped[str] = mapped_column(nullable=False)
    user_type: Mapped[UserType] = mapped_column(
        nullable=False, default=UserType.guest.value
    )

    __mapper_args__ = {"polymorphic_identity": "users", "polymorphic_on": user_type}
#
controller.py
 @get(
        path="/employee",
        return_dto=ReadEmployeeDTO,
    )
    async def get_profile_employee(
            self,
            request: Request,
            employee_transaction: EmployeeService,
            user_transaction: UserService,
    ) -> EmployeeModel:

        user_object = await employee_transaction.get(request.user.id)
        print(f"USER: {user_object}")
        return  employee_transaction.to_schema(user_object)```
#

receiving this error:
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)

cedar pike
#

nvm i manage to make it work by using this
user_object.to_dict(exclude=exclude_fields)

but i dont know which is better for returning data DTO or the exclude?

wise tartan
#

@hoary igloo can you comment on this?

hoary igloo
#

It depends. This is about lazy loading of data