Код: Выделить всё
class UserPydantic(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str = Field(...)
email: str = EmailStr()
is_active: bool = Field(default=True)
is_admin: bool = Field(default=False)
created_at: datetime = Field(...)
Код: Выделить всё
class UserDB(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String, index=True)
email = Column(String, index=True, nullable=False, unique=True)
hashed_password = Column(String(length=255), nullable=False)
is_active = Column(Boolean, default=True, nullable=False)
is_admin = Column(Boolean, default=False, nullable=False)
created_at = Column(DateTime, default=current_timestamp(), nullable=False)
instagram_dms: Mapped[list["InstagramDmDB"]] = relationship("InstagramDmDB",
back_populates="user")
def to_pydantic(self) -> UserPydantic:
return UserPydantic(
name=self.name,
email=self.email,
is_active=self.is_active,
is_admin=self.is_admin,
created_at=self.created_at
)
Это вызывает дополнительные проблемы с моим токсикологическим тестированием mypy.
Подробнее здесь: https://stackoverflow.com/questions/777 ... h-pydantic