-
First Check
Commit to Help
Example Codefrom typing import List, Optional, Any
from sqlmodel import Field, SQLModel as _SQLModel, Relationship
from slugify import slugify
from datetime import date, datetime
from pydantic import validator
from sqlalchemy.orm import declared_attr
class SQLModel(_SQLModel):
@declared_attr # type: ignore
def __tablename__(cls) -> str:
return cls.__name__
class Config:
use_enum_values = True
class BaseIDModel(SQLModel):
id: Optional[int] = Field(
primary_key=True,
index=True,
nullable=False,
)
class ProjectBase(SQLModel):
is_active: bool = Field(default=True, nullable=False)
name: str = Field(nullable=False)
publish_date: date = Field(
default_factory=datetime.now().date, nullable=False)
repr_name: Optional[str] = Field(default=None, nullable=False)
approved: bool = Field(default=False, nullable=False)
featured: bool = Field(default=False, nullable=False)
# post_init
@validator("repr_name", pre=True, always=True)
def slugify_name(cls, v, values) -> str:
if v is None:
print(f"{v=}, {values=}")
return slugify(values["name"], separator="_")
raise ValueError(
f"`repr_name` cannot be passed in as a parameter to the `Request`.")
class Project(BaseIDModel, ProjectBase, table=True):
pass
class IProjectCreate(ProjectBase):
pass
if __name__ == '__main__':
Project.from_orm(IProjectCreate(name='Hello A')) Description
Operating SystemWindows Operating System DetailsLocal machine is Windows but running the FastAPI application via docker, i.e. Linux OS. SQLModel Version'0.0.6' Python VersionPython 3.10.6 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
jonra1993/fastapi-alembic-sqlmodel-async#46 The same issue is also mentioned here. |
Beta Was this translation helpful? Give feedback.
-
Hey, anyway I fixed my issue by noticing that validations belong to pydantic's "schema" instead of sqlalchemy's "model", hence, the |
Beta Was this translation helpful? Give feedback.
Hey, anyway I fixed my issue by noticing that validations belong to pydantic's "schema" instead of sqlalchemy's "model", hence, the
@validator
decorator should be applied to validating methods belong to the schemas classes, instead of models classes.