API

base_model Module

BaseModel

class sqlalchemy_dict.base_model.BaseModel

BaseModel provides sqlalchemy_dict abilities ready for every sqlalchemy declarative models.

This class should set to sqlalchemy declarative_base as base class like this:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.schema import MetaData

metadata = MetaData()
DeclarativeBase = declarative_base(cls=BaseModel, metadata=metadata)
__formatter__ = <class 'sqlalchemy_dict.formatter.DefaultFormatter'>
classmethod dump_query(query: sqlalchemy.orm.query.Query) → List[dict]

Dump query results in a list of model dictionaries.

Parameters

query

Returns

classmethod expose(func: Callable) → Callable

A decorator to automatically convert model instance or query to dictionary or list of dictionaries.

Parameters

func

Returns

classmethod extract_data_from_dict(context: dict) → Generator[[Tuple[sqlalchemy.sql.schema.Column, Any], None], None]

Extract values from dictionary.

Parameters

context

Returns

Tuple of diction

classmethod get_column(column: Union[sqlalchemy.sql.schema.Column, str])

Get column by its name, also accept Column type too.

Parameters

column

Returns

classmethod get_column_info(column: sqlalchemy.sql.schema.Column) → dict

Get column info, it will merge info from proxy properties

New in version 0.4.0.

Parameters

column

Returns

classmethod get_dict_key(column: sqlalchemy.sql.schema.Column) → str

Get column dictionary key.

it uses column info if already dict_key was set.

Parameters

column

Returns

classmethod import_value(column: Union[sqlalchemy.sql.schema.Column, str], v)

Import value for a column. :param column: :param v: :return:

classmethod iter_columns(relationships=True, synonyms=True, composites=True, use_inspection=True, hybrids=True) → Generator[[sqlalchemy.sql.schema.Column, None], None]

Iterate model columns.

Parameters
  • relationships – Include relationships

  • synonyms – Include synonyms

  • composites – Include composites

  • use_inspection – Force to use sqlalchemy inspector

  • hybrids – Include hybrids

Returns

classmethod iter_dict_columns(include_readonly_columns=True, include_protected_columns=False, **kw) → Generator[[sqlalchemy.sql.schema.Column, None], None]

Same as BaseModel.iter_columns() but have options to include readonly and protected columns.

Parameters
  • include_readonly_columns

  • include_protected_columns

  • kw

Returns

classmethod prepare_for_export(column: sqlalchemy.sql.schema.Column, v) → tuple

Prepare column value to export.

Parameters
  • column

  • v

Returns

Returns tuple of column dictionary key and value

to_dict() → dict

Convert model instance to dictionary.

Returns

update_from_dict(context: dict)

Update model instance from dictionary.

Parameters

context

Returns

formatter Module

Formatter

class sqlalchemy_dict.formatter.Formatter

Model formatter abstract class

classmethod export_date(value: datetime.date)

Export python date

Parameters

value

Returns

classmethod export_datetime(value: datetime.datetime)

Export python datetime

Parameters

value

Returns

classmethod export_key(key)

Export dictionary key

Parameters

key – Model field name

Returns

classmethod export_time(value: datetime.time)

Export python time

Parameters

value

Returns

classmethod import_date(value: [<class 'str'>, <class 'int'>]) → datetime.date

Import date

Parameters

value

Returns

classmethod import_datetime(value: [<class 'str'>, <class 'int'>]) → datetime.datetime

Import datetime field

Parameters

value

Returns

classmethod import_time(value: [<class 'str'>, <class 'int'>]) → datetime.time

Import time

Parameters

value

Returns

DefaultFormatter

class sqlalchemy_dict.formatter.DefaultFormatter

Default Formatter

field Module

Field

class sqlalchemy_dict.field.Field(*args, dict_key: str = None, readonly: bool = None, protected: bool = None, info: dict = None, **kwargs)
An overridden class from sqlalchemy.Column to apply sqlalchemy_dict

properties.

__init__(*args, dict_key: str = None, readonly: bool = None, protected: bool = None, info: dict = None, **kwargs)

Initialize the field

Parameters
  • args – Positional-arguments that directly pass into sqlalchemy.Column.__init__

  • dict_key – Custom dictionary key related to this field, as default it will reads the field name and format (using sqlalchemy_dict.BaseModel.__formatter__) it before export.

  • readonly – Make field read-only, it’s mean this field will not accept any value from sqlalchemy_dict.BaseModel.update_from_dict input dictionary.

  • protected – Make field protected to representation

  • info – Pass into Column info

  • kwargs – Keyword-arguments that directly pass into sqlalchemy.Column.__init__

relationship

sqlalchemy_dict.field.relationship(*args, dict_key: str = None, protected: bool = None, **kwargs)

Same as sqlalchemy.orm.relationship with extra arguments to use in sqlalchemy_dict.

Parameters
  • args – Positional-arguments that directly pass into sqlalchemy.orm.relationship.

  • dict_key – Custom dictionary key. default is formatted (using sqlalchemy_dict.BaseModel.__formatter__) attribute name (where relationship called).

  • protected – Make field protected to representation.

  • kwargs – Keyword-arguments that directly pass into sqlalchemy.orm.relationship.

Returns

composite

sqlalchemy_dict.field.composite(*args, dict_key: str = None, protected: bool = None, readonly: bool = None, **kwargs)

Same as sqlalchemy.orm.composite with extra arguments to use in sqlalchemy_dict.

Parameters
  • args – Positional-arguments that directly pass into sqlalchemy.orm.composite.

  • dict_key – Custom dictionary key. default is formatted (using sqlalchemy_dict.BaseModel.__formatter__) attribute name (where composite called).

  • protected – Make field protected to representation.

  • readonly – Make field read-only, it’s mean this field will not accept any value from sqlalchemy_dict.BaseModel.update_from_dict input dictionary.

  • kwargs – Keyword-arguments that directly pass into sqlalchemy.orm.composite.

Returns

synonym

sqlalchemy_dict.field.synonym(*args, dict_key: str = None, protected: bool = None, readonly: bool = None, **kwargs)

Same as sqlalchemy.orm.synonym with extra arguments to use in sqlalchemy_dict.

New in version 0.4.0.

Parameters
  • args – Positional-arguments that directly pass into sqlalchemy.orm.synonym.

  • dict_key – Custom dictionary key. default is formatted (using sqlalchemy_dict.BaseModel.__formatter__) attribute name (where synonym called).

  • protected – Make field protected to representation.

  • readonly – Make field read-only, it’s mean this field will not accept any value from sqlalchemy_dict.BaseModel.update_from_dict input dictionary.

  • kwargs – Keyword-arguments that directly pass into sqlalchemy.orm.synonym.

Returns