Exclude field from schema #1666
-
First Check
Commit to Help
Example Codeimport uuid as uuid_pkg
from datetime import datetime
from typing import Optional
from pydantic import field_validator
from sqlmodel import Column, DateTime, Field, SQLModel
class HistorySCD2Base(SQLModel):
"""
Base model for historical tracking.
SCD Type 2 implementation.
"""
# model_config = {
# "json_schema_extra": {
# "exclude": True
# }
# }
h_group_id: uuid_pkg.UUID = Field(
default_factory=uuid_pkg.uuid4,
exclude=True,
index=True,
nullable=False,
description="Id of the group of historical records",
)
h_start_date: Optional[datetime] = Field(
default_factory=datetime.now,
exclude=True,
sa_column=Column(DateTime(timezone=False)),
)
h_end_date: Optional[datetime] = Field(
default_factory=datetime.now,
exclude=True,
sa_column=Column(DateTime(timezone=False)),
)
h_is_current: Optional[bool] = Field(
default=None,
exclude=True,
)
@field_validator("h_start_date", "h_end_date", mode="before")
@classmethod
def parse_datetime(cls, v):
"""Parse ISO formatted datetime strings to datetime objects."""
if isinstance(v, str):
return datetime.fromisoformat(v.replace("Z", "+00:00"))
return v
class AddressBase(HistorySCD2Base):
"""Base model for addresses."""
# ... fields of address
pass
class AddressUpdate(AddressBase):
"""Model for updating an address."""
pass
class Address(AddressBase, table=True):
"""
Database model for addresses.
"""
__tablename__ = "addresses"
# id and othersDescriptionAddress-Input displays Problem description The Address-Input component is displaying fields such as Expected behaviour The component should respect the schema and not display fields marked as excluded or that are part of internal metadata. Steps to reproduce
Impact
Proposed solution
Operating SystemLinux Operating System DetailsNo response SQLModel Version0.0.27 Python VersionPython 3.13.9 Additional Context
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
This is a common source of confusion, but It comes from Pydantic and here is a description of
See: https://docs.pydantic.dev/latest/concepts/fields/#excluding-fields |
Beta Was this translation helpful? Give feedback.

This is a common source of confusion, but
excludeparameter is only applicable for serialization, not validation.It comes from Pydantic and here is a description of
excludeparameter in Pydantic's docs:See: https://docs.pydantic.dev/latest/concepts/fields/#excluding-fields