Skip to content

InspectionMixin

The InspectionMixin class provides attributes and properties inspection functionality for SQLAlchemy models.

Info

This mixin is intended to extend the functionality of the SmartQueryMixin and SerializationMixin. It is not intended to be used on its own.

Table of Contents

Instance Methods

repr

def __repr__(self)

Print the model in a readable format including the primary key.

Format:

   <ClassName #PrimaryKey>

Example:

user = await User.get(id=1)
print(user)
# User #1
users = await User.find(username__endswith='Doe').all()
print(users)
# [<User #1>, <User #2>]

Class Methods

get_class_of_relation

def get_class_of_relation(relation_name: str)

Gets the class of a relationship by its name.

Parameters:

  • relation_name: The name of the relationship.

Returns:

  • type: The class of the relationship.

Example:

user = await User.get(id=1)
user.get_class_of_relation('posts')
# <class 'Post'>

Properties

id_str

Returns primary key as string.

If there is a composite primary key, returns a hyphenated string, as follows: '1-2-3'.

If there is no primary key, returns 'None'.

bob = User.create(name='Bob')
print(bob.id_str)
# 1

columns

Sequence of string key names for all columns in this collection.

print(User.columns)
# ['id', 'username', 'name', 'age', 'created_at', 'updated_at']

primary_keys_full

Gets primary key properties for a SQLAlchemy cls.

Taken from marshmallow_sqlalchemy.

print(User.primary_keys_full)
# [<sqlalchemy.orm.attributes.InstrumentedAttribute 'id'>]

primary_keys

Returns a list of primary key names.

print(User.primary_keys)
# ['id']

relations

Returns a list of relationship names.

print(User.relations)
# ['posts', 'comments']

settable_relations

Returns a list of settable relationship names.

print(User.settable_relations)
# ['posts', 'comments']

hybrid_properties

Returns a list of hybrid property names.

print(User.hybrid_properties)
# ['is_adult']

hybrid_methods_full

Returns a dict of hybrid methods.

print(User.hybrid_methods_full)
# {'is_adult': <sqlalchemy.orm.attributes.InstrumentedAttribute 'is_adult'>}

hybrid_methods

Returns a list of hybrid method names.

print(User.hybrid_methods)
# ['is_adult']

filterable_attributes

Returns a list of filterable attributes.

These are all columns, relations and hybrid properties.

print(User.filterable_attributes)
# ['id', 'username', 'name', 'age', 'created_at', 'updated_at', 'posts', 'comments', 'is_adult']

sortable_attributes

Returns a list of sortable attributes.

These are all columns and hybrid properties.

print(User.sortable_attributes)
# ['id', 'username', 'name', 'age', 'created_at', 'updated_at', 'is_adult']

settable_attributes

Returns a list of settable attributes.

These are all columns, settable relations and hybrid properties.

print(User.settable_attributes)
# ['id', 'username', 'name', 'age', 'created_at', 'updated_at', 'posts', 'comments', 'is_adult']