from pydantic import BaseModel, ConfigDict, Field, model_validator from datetime import datetime class TaskCommentCreate(BaseModel): model_config = ConfigDict(extra="forbid") content: str = Field(min_length=1, max_length=10000) class TaskCommentResponse(BaseModel): id: int task_id: int user_id: int | None = None author_name: str | None = None content: str created_at: datetime model_config = ConfigDict(from_attributes=True) @model_validator(mode="before") @classmethod def resolve_author_name(cls, data): # type: ignore[override] """Populate author_name from eagerly loaded user relationship.""" if hasattr(data, "user") and data.user is not None and not getattr(data, "author_name", None): cols = {c.key: getattr(data, c.key) for c in data.__table__.columns} cols["author_name"] = data.user.username return cols return data