{"id":48334329,"url":"https://github.com/yarbshk/sqlalchemy-dst","last_synced_at":"2026-04-05T01:37:09.906Z","repository":{"id":53667301,"uuid":"117309635","full_name":"yarbshk/sqlalchemy-dst","owner":"yarbshk","description":"SQLAlchemy dictionary de/serialization tool.","archived":false,"fork":false,"pushed_at":"2021-03-19T23:06:57.000Z","size":26,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-20T13:21:04.140Z","etag":null,"topics":["multidimensional-data","python","serialization-library","sqlalchemy"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yarbshk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-13T03:11:38.000Z","updated_at":"2023-01-24T14:18:24.000Z","dependencies_parsed_at":"2022-09-19T10:02:02.754Z","dependency_job_id":null,"html_url":"https://github.com/yarbshk/sqlalchemy-dst","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/yarbshk/sqlalchemy-dst","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarbshk%2Fsqlalchemy-dst","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarbshk%2Fsqlalchemy-dst/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarbshk%2Fsqlalchemy-dst/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarbshk%2Fsqlalchemy-dst/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yarbshk","download_url":"https://codeload.github.com/yarbshk/sqlalchemy-dst/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yarbshk%2Fsqlalchemy-dst/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31421869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-05T00:25:07.052Z","status":"ssl_error","status_checked_at":"2026-04-05T00:25:05.923Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["multidimensional-data","python","serialization-library","sqlalchemy"],"created_at":"2026-04-05T01:37:09.374Z","updated_at":"2026-04-05T01:37:09.891Z","avatar_url":"https://github.com/yarbshk.png","language":"Python","readme":"# SQLAlchemy Dictionary Serialization Tools\n\nSQLAlchemy DST is a set of must have functions and classes for the dictionary serialisation. It allows you to quickly convert a SQLAlchemy model to a python dictionary and vise versa. \n\nWhy use it? Well, for example, I use this tools to reduce amount of requests to a database. So, this is a simple kind of caching complex objects in a session.\n\n## Requirements\nThis module successfully passed unit testing on [Debian Jessie](https://wiki.debian.org/DebianJessie) with **Python 2.7** and **3.5** versions.\n\nDepends on the following packages (see `requirements.txt`):\n- [sqlalchemy](https://www.sqlalchemy.org/) \u003e= 0.9.4\n\n## Installation\nThis module is available as [PyPi package](https://pypi.python.org/pypi/sqlalchemy-dst), therefore you can install one as follows:\n\n```bash\n$ pip install sqlalchemy-dst\n```\n\n## Getting started\n\nSQLAlchemy DST use only two functions (`row2dict()` and `dict2row()`) to serialize objects. Let's see how it works...\n\nImagine you have a User model with several relations (Role, Permissions), most likely, you will want to store instance of the model in a web server session for efficency purposes. Unfortunatelly, it's not possible to store instances of Python classes in the session. So, you need to **serialize** one **into a dictionary** first:\n```python\n\u003e\u003e\u003e from sqlalchemy_dst import dict2row, row2dict\n\u003e\u003e\u003e from tests.main import db\n\u003e\u003e\u003e from tests.schema import Permission, Role, User\n\u003e\u003e\u003e user = db.session.query(User).first() # create an instance of the User model\n```\nSerialize the instance of the User model into the one-dimensional dictionary:\n```python\n\u003e\u003e\u003e row2dict(user)\n{'username': 'yarbshk', 'role': None, 'role_id': 1, '_secret': 'x', 'password': 'x', 'id': 1}\n```\nSerialize the instance of the User model into the three-dimensional dictionary and exclude a few attributes using different methods:\n```python\n\n\u003e\u003e\u003e row2dict(user, depth=3, exclude={'role_id', '_secret'})\n{'username': 'yarbshk', 'password': 'x', 'id': 1, 'role': {'description': None, 'id': 1, 'permissions': [{'id': 1, 'name': 'posts:r'}, {'id': 2, 'name': 'posts:w'}], 'name': 'Moderator'}}\n\u003e\u003e\u003e row2dict(user, depth=3, exclude_pk=True, exclude_underscore=True)\n{'username': 'yarbshk', 'password': 'x', 'id': 1, 'role': {'description': None, 'id': 1, 'permissions': [{'id': 1, 'name': 'posts:r'}, {'id': 2, 'name': 'posts:w'}], 'name': 'Moderator'}}\n\n```\nSerialize the instance of the User model and store one in a local variable to work with:\n```python\n\u003e\u003e\u003e user_dict = row2dict(user, depth=3)\n\u003e\u003e\u003e user_dict\n{'username': 'yarbshk', 'role': {'description': None, 'id': 1, 'permissions': [{'id': 1, 'name': 'posts:r'}, {'id': 2, 'name': 'posts:w'}], 'name': 'Moderator'}, 'role_id': 1, '_secret': 'x', 'password': 'x', 'id': 1}\n```\n\nSuppose in other part of the application you need to **deserialize that dictionary** to be able to use all features of the User's model (e.g. calling methods, querying data):\n```python\n\u003e\u003e\u003e dict2row(user_dict, User)\n\u003cUser (transient 139927902911456)\u003e\n```\nThe example above will create new instance of the User model without any relations (such as _role_ or _permissions_). To create a user's instance with all relations it's necessary to explicitly specify the models (Role, Permission) for that relations:\n```python\n\u003e\u003e\u003e user_row = dict2row(user_dict, User, rel={'role': Role, 'permissions': Permission})\n\u003cUser (transient 139927902904272)\u003e\n```\nWell, now you are able to do any manipulations with this instance in the same way as before serialization:\n```python\n\u003e\u003e\u003e [p.name for p in user_row.role.permissions]\n['posts:r', 'posts:w']\n```\n\nAwesome, isn't it? :)\n\n## Pay attention\n\nIf you want to exclude some attribute of model which has synonym (and vice versa) you MUST exclude both attribute and synonym (in other case SQLAlchemy automatically sets the same value for attribute and their synonym even if one of them is excluded):\n\n```python\n\u003e\u003e\u003e dict2row(user_dict, User, exclude={'_secret', 'password'})\n```\n\n## Documentation\n\n\u003e Note that **default values** of the optional arguments in methods below **are setting implicitly**. This behavior is required by class DictionarySerializableModel (see detailed explanation below).\n\n### row2dict(row, depth=None, exclude=None, exclude_pk=None, exclude_underscore=None, only=None, fk_suffix=None)\n\nRecursively walk row attributes to serialize ones into a dict.\n\n- **row** (required) – _instance_ of the declarative base class (base SQLAlchemy model).\n- **depth** (optional, default: `1`) – _number_ that represent the depth of related relationships.\n- **exclude** (optional, default: `set()`) – _set_ of attributes names to exclude.\n- **exclude_pk** (optional, default: `False`) – _are_ foreign keys (e.g. fk_name_id) excluded.\n- **exclude_underscore** (optional, default: `False`) – _are_ private and protected attributes excluded.\n- **only** (optional, default: `set()`) – _set_ of attributes names to include.\n- **fk_suffix** (optional, default: `_id`) – _str_ that represent a foreign key suffix.\n\n### dict2row(d, model, rel=None, exclude=None, exclude_pk=None, exclude_underscore=None, only=None, fk_suffix=None)\n\nRecursively walk dict attributes to serialize ones into a row.\n\n- **d** (required) – _dict_ which represent a serialized row.\n- **model** (required) – _class_ nested from the declarative base class.\n- **rel** (optional, default: `dict()`) – _dict_ of key (relationship name) -value (class) pairs.\n- **exclude** (optional, default: `set()`) – _set_ of attributes names to exclude.\n- **exclude_pk** (optional, default: `False`) – _are_ foreign keys (e.g. fk_name_id) excluded.\n- **exclude_underscore** (optional, default: `False`) – _are_ private and protected attributes excluded.\n- **only** (optional, default: `set()`) – _set_ of attributes names to include.\n- **fk_suffix** (optional, default: `_id`) – _str_ that represent a foreign key suffix.\n\n### DictionarySerializableModel\n\nClass that extends serialization functionality of your models.\n\n- **as_dict**(**kwargs) – wrapper around the `row2dict()` method – where _kwargs_ are mapped optional arguments for the `row2dict()` method. \n- **from_dict**(d, **kwargs) – wrapper around the `dict2row()` method – where _d_ is source dictionary, _kwargs_ are mapped optional arguments for the `dict2row()` method.\n- **\\_\\_iter\\_\\_**() – object that can be iterated upon (it uses dictionary serialized by the `row2dict()` method).\n\nUse it as a base class for the `sqlalchemy.ext.declarative_base()` method (try to explore the `cls` argument in depth).\n\nIf you decide to use the `DictionarySerializableModel` class as a base model, you may keep frequently used arguments values of the serialization methods in your model(s). Just set necessary **configuraion attributes** in your model(s) as follows:\n\n```python\nfrom tests.main import db\n...\nclass User(db.Model):\n    ...\n    _sa_dst_exclude_pk = True\n    _sa_dst_exclude_underscore = True\n    ...\n```\n\nThe class declaration above is equal to calling the following method:\n\n```python\n\u003e\u003e\u003e user.as_dict(exclude_pk=True, exclude_underscore=True)\n{'username': 'yarbshk', 'id': 1, 'password': 'x', 'role': None}\n``` \n\nAfter, we're simply calling the method below without any parameters, because configuration attributes already are set in the model above:\n\n```python\n\u003e\u003e\u003e user.as_dict()\n{'username': 'yarbshk', 'id': 1, 'password': 'x', 'role': None}\n```\n\nThe list of available **configuration attributes**:\n* _sa_dst_depth\n* _sa_dst_exclude\n* _sa_dst_exclude_pk\n* _sa_dst_exclude_underscore\n* _sa_dst_only\n* _sa_dst_rel\n* _sa_dst_fk_suffix\n\nYou can see an example of instantiating Flask + SQLAlchemy with `DictionarySerializableModel` in the `tests/main.py` file. \n\n## Copyright and License\nCopyright (c) 2018 Yuriy Rabeshko. Code released under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyarbshk%2Fsqlalchemy-dst","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyarbshk%2Fsqlalchemy-dst","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyarbshk%2Fsqlalchemy-dst/lists"}