Repository navigation
Accessing and displaying the Field description attribute #246
Copy link
Copy link
Closed
Labels
questionFurther information is requestedFurther information is requested
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Feb 15, 2022 By accessing your model's
__fields__attribute?for k, v in Contract.__fields__.items(): print(f"{k}: {v.field_info.description}")Reacted by Christian Staudt and blerrgh@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.
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).
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"
Reacted by Bruno Bernardes da Costa- locked and limited conversation to collaborators
on Aug 12, 2025
Metadata
Metadata
Assignees
Labels
questionFurther information is requestedFurther information is requested
First Check
Commit to Help
Example Code
Description
I was happy to find out that every attribute of a SQLModel class can be documented with
... = Field(description="attribute meaning here"). Let's do more of that. However, I was confused when I tried to access and display the field description and didn't find a way to do that.Given the example class
Contract, accessingContract.signature_date.descriptiongives the following output:'signature_date'Field(description=...)argument?Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
Python 3.9.7
Additional Context
No response