-
First Check
Commit to Help
Example Codeclass Contract(SQLModel, table=True):
"""A contract defines the business conditions of a project"""
id: Optional[int] = Field(default=None, primary_key=True)
title: str = Field(description="Short description of the contract.")
client: Client = Relationship(
back_populates="contracts",
)
signature_date: datetime.date = Field(
description="Date on which the contract was signed",
)
start_date: datetime.date = Field(
description="Date from which the contract is valid",
)
end_date: Optional[datetime.date] = Field(
description="Date until which the contract is valid",
) DescriptionI was happy to find out that every attribute of a SQLModel class can be documented with Given the example class
Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.6 Python VersionPython 3.9.7 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
By accessing your model's
UPD YuriiMotov: use print(Contract.model_fields["title"]) |
Beta Was this translation helpful? Give feedback.
-
@byrman Using ´Model.fields` works fine, thanks. This feature may be a bit hidden. I'll leave it to others to decide whether there is a bug in here. |
Beta Was this translation helpful? Give feedback.
-
I've been looking for the same thing. Not sure I'd call it a bug but it would sure be cool if the Field description could be more tightly integrated with e.g. Pylance (that's probably a job for their maintainers and I'll drop a comment over there). |
Beta Was this translation helpful? Give feedback.
-
I had to figure this out recently, so here's what I ended up doing in case it helps anyone: from typing import Any, TypeVar
from sqlmodel import Field, SQLModel
from sqlmodel.main import FieldInfo, SQLModelMetaclass
ColumnInstrumentedAttr = TypeVar("ColumnInstrumentedAttr")
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True, description="The Hero's ID")
name: str = Field(description="The Hero's name", alias="legal_name")
secret_name: str = Field(description="Shhh! It's the Hero's secret identity!")
age: int | None = Field(default=None, description="The Hero's age")
def colAttr(columnAttr: ColumnInstrumentedAttr, fieldName: str, defaultValue: Any = None) -> Any:
if hasattr(columnAttr, fieldName):
return getattr(columnAttr, fieldName)
return defaultValue
def colAttrFromModelFields(columnAttr: ColumnInstrumentedAttr, fieldName: str, defaultValue: Any = None) -> Any:
sqlModelMetaClass: SQLModelMetaclass = getattr(columnAttr, "class_")
modelFields: dict[str, FieldInfo] = sqlModelMetaClass.model_fields
fieldKey: str = getattr(columnAttr, "key")
fieldInfo: FieldInfo = modelFields[fieldKey]
if hasattr(fieldInfo, fieldName):
return getattr(fieldInfo, fieldName)
return defaultValue
def colName(columnAttr: ColumnInstrumentedAttr) -> str:
return colAttr(columnAttr, "name")
def colDesc(columnAttr: ColumnInstrumentedAttr) -> str:
return colAttrFromModelFields(columnAttr, "description")
print(colName(Hero.id)) # prints "id"
print(colName(Hero.name)) # prints "name"
print(colDesc(Hero.secret_name)) # prints "Shhh! It's the Hero's secret identity!"
print(colDesc(Hero.age)) # prints "The Hero's age"
print(colAttr(Hero.id, "primary_key")) # prints "True"
print(colAttr(Hero.age, "default")) # prints "None"
print(colAttrFromModelFields(Hero.name, "alias")) # prints "legal_name"
print(colAttrFromModelFields(Hero.name, "something_else", "dflt")) # prints "dflt" |
Beta Was this translation helpful? Give feedback.
By accessing your model's
__fields__
attribute?UPD YuriiMotov: use
model_fields
in Pydantic V2