models.py 505 B

123456789101112131415161718192021222324
  1. from sqlmodel import SQLModel, Field, create_engine
  2. engine = create_engine(
  3. 'sqlite:///db.sqlite',
  4. echo=True,
  5. connect_args={
  6. 'check_same_thread': False
  7. }
  8. )
  9. class Hero(SQLModel, table=True):
  10. id: int | None = Field(default=None, primary_key=True)
  11. name: str
  12. secret_name: str
  13. age: int | None = None
  14. @property
  15. def full_name(self) -> str:
  16. return f'{self.name} ({self.secret_name})'
  17. def create_tables(engine):
  18. SQLModel.metadata.create_all(engine)