Inspection Mixin
The InspectionMixin
class provides attributes and properties inspection
functionality for SQLAlchemy models.
Note
This mixin is intended to extend the functionality of the
Smart Queries
and
Serialization
mixins.
It is not intended to be used on its own.
Info
The examples below assume the following models:
from sqlalchemy import ForeignKey, String
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlactive.base_model import ActiveRecordBaseModel
class BaseModel(ActiveRecordBaseModel):
__abstract__ = True
class User(BaseModel):
__tablename__ = 'users'
id: Mapped[int] = mapped_column(
primary_key=True, autoincrement=True, index=True
)
username: Mapped[str] = mapped_column(
String(18), nullable=False, unique=True
)
name: Mapped[str] = mapped_column(String(50), nullable=False)
age: Mapped[int] = mapped_column(nullable=False)
posts: Mapped[list['Post']] = relationship(back_populates='user')
comments: Mapped[list['Comment']] = relationship(back_populates='user')
@hybrid_property
def is_adult(self) -> int:
return self.age > 18
@hybrid_method
def older_than(self, other: 'User') -> bool:
return self.age > other.age
class Post(BaseModel):
__tablename__ = 'posts'
id: Mapped[int] = mapped_column(
primary_key=True, autoincrement=True, index=True
)
title: Mapped[str] = mapped_column(String(100), nullable=False)
body: Mapped[str] = mapped_column(nullable=False)
rating: Mapped[int] = mapped_column(nullable=False)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
user: Mapped['User'] = relationship(back_populates='posts')
comments: Mapped[list['Comment']] = relationship(back_populates='post')
class Comment(BaseModel):
__tablename__ = 'comments'
id: Mapped[int] = mapped_column(
primary_key=True, autoincrement=True, index=True
)
body: Mapped[str] = mapped_column(nullable=False)
post_id: Mapped[int] = mapped_column(ForeignKey('posts.id'))
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
post: Mapped['Post'] = relationship(back_populates='comments')
user: Mapped['User'] = relationship(back_populates='comments')
class Product(BaseModel):
__tablename__ = 'products'
id: Mapped[int] = mapped_column(
primary_key=True, autoincrement=True, index=True
)
name: Mapped[str] = mapped_column(String(100), nullable=False)
description: Mapped[str] = mapped_column(String(100), nullable=False)
price: Mapped[float] = mapped_column(nullable=False)
sells: Mapped[list['Sell']] = relationship(
back_populates='product', viewonly=True
)
class Sell(BaseModel):
__tablename__ = 'sells'
id: Mapped[int] = mapped_column(primary_key=True)
product_id: Mapped[int] = mapped_column(
ForeignKey('products.id'), primary_key=True
)
quantity: Mapped[int] = mapped_column(nullable=False)
product: Mapped['Product'] = relationship(back_populates='sells')
API Reference
Properties
id_str
Returns a string representation of the primary key.
If the primary key is composite, returns a comma-separated list of key-value pairs.
Examples
Class Properties
columns
Returns a list of column names.
Examples
string_columns
Returns a list of string column names.
Examples
primary_keys_full
Returns the columns that form the primary key.
Examples
primary_keys
Returns the names of the primary key columns.
Examples
primary_key_name
Returns the primary key name of the model.
Warning
This property can only be used if the model has a single primary key. If the model has a composite primary key, an
CompositePrimaryKeyError
is raised.Raises
CompositePrimaryKeyError
: If the model has a composite primary key.Examples
relations
Returns a list of relationship names.
Examples
settable_relations
Returns a list of settable (not viewonly) relationship names.
Examples
hybrid_properties
Returns a list of hybrid property names.
Examples
hybrid_methods_full
Returns a dict of hybrid methods.
Examples
hybrid_methods
Returns a list of hybrid method names.
Examples
filterable_attributes
Returns a list of filterable attributes.
These are all columns, relations, hybrid properties and hybrid methods.
Examples
sortable_attributes
Returns a list of sortable attributes.
These are all columns and hybrid properties.
Examples
settable_attributes
Returns a list of settable attributes.
These are all columns, settable relations and hybrid properties.
Examples
searchable_attributes
Returns a list of searchable attributes.
These are all string columns.
Examples
Instance Methods
repr
Returns a string representation of the model.
Representation format is
ClassName(pk1=value1, pk2=value2, ...)
Examples
Class Methods
get_class_of_relation
Gets the class of a relationship by its name.
Parameters
relation_name
: The name of the relationship.Returns
type[Self]
: The class of the relationship.Raises
RelationError
: If the relation is not found.Examples