Overview
AbstractEntity is the base class for all ORM entities in Framefox. It extends SQLModel and provides utility methods for generating Pydantic models for various CRUD operations.
Class Definition
from framefox.core.orm import AbstractEntity
class AbstractEntity(SQLModel):
__abstract__ = True
Methods
generate_create_model
Generates a Pydantic model class for creating new entities, excluding the id field.
@classmethod
def generate_create_model(cls) -> Type[BaseModel]
cls
Type[AbstractEntity]
required
The entity class for which to generate the create model
A dynamically created Pydantic model class with all fields except id, named {EntityName}Create
Example
from framefox.core.orm import AbstractEntity
from sqlmodel import Field
class User(AbstractEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str
email: str
age: int
# Generate the create model
UserCreate = User.generate_create_model()
# Use it for validation
user_data = UserCreate(username="john_doe", email="john@example.com", age=25)
get_primary_keys
Returns the list of primary key column names for the entity.
@classmethod
def get_primary_keys(cls: Type[SQLModel]) -> List[str]
The entity class to inspect for primary keys
A list of primary key column names
Example
class User(AbstractEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str
# Get primary keys
primary_keys = User.get_primary_keys()
print(primary_keys) # Output: ['id']
generate_find_model
Generates a Pydantic model class containing only the primary key fields, useful for finding entities.
@classmethod
def generate_find_model(cls) -> Type[BaseModel]
cls
Type[AbstractEntity]
required
The entity class for which to generate the find model
A dynamically created Pydantic model class with only primary key fields, named {EntityName}Find
Example
class User(AbstractEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str
email: str
# Generate the find model
UserFind = User.generate_find_model()
# Use it to specify which entity to find
find_params = UserFind(id=123)
generate_patch_model
Generates a Pydantic model class with all non-primary-key fields set as optional, useful for partial updates.
@classmethod
def generate_patch_model(cls) -> Type[BaseModel]
cls
Type[AbstractEntity]
required
The entity class for which to generate the patch model
A dynamically created Pydantic model class with all non-primary-key fields as optional, named {EntityName}Patch
Example
class User(AbstractEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str
email: str
age: int
# Generate the patch model
UserPatch = User.generate_patch_model()
# Use it for partial updates
update_data = UserPatch(email="newemail@example.com") # Only update email
Complete Example
from framefox.core.orm import AbstractEntity
from sqlmodel import Field
class Product(AbstractEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
price: float
stock: int
description: str | None = None
# Generate all model types
ProductCreate = Product.generate_create_model()
ProductFind = Product.generate_find_model()
ProductPatch = Product.generate_patch_model()
# Create a new product
new_product = ProductCreate(
name="Laptop",
price=999.99,
stock=50,
description="High-performance laptop"
)
# Find a product by ID
find_params = ProductFind(id=1)
# Partially update a product
update_data = ProductPatch(stock=45) # Only update stock
# Get primary keys
print(Product.get_primary_keys()) # Output: ['id']
Notes
- All entity classes should inherit from
AbstractEntity to gain access to these utility methods
- The
__abstract__ = True attribute ensures this class won’t be mapped to a database table
- Generated models are created dynamically using Pydantic’s
create_model function
- These generated models are particularly useful for API request/response validation