skillfed

sqlalchemy-serializer

Mixin for SQLAlchemy models serialization without pain

sqlalchemy-serializer v1.6.3 164.0K downloads/30d#10,558 on PyPI201
Permissive license MIT Active released

What it is and what it does

SQLAlchemy-serializer is a mixin that adds a `.to_dict()` method to SQLAlchemy model instances, turning them into dictionaries suitable for JSON responses or data export. It handles nested relationships automatically, includes support for both traditional and modern SQLAlchemy 2.0 annotation styles, and offers fine-grained control over which fields to include or exclude via `rules`, `only`, and `exclude_values` parameters.

The package is designed for developers who need simple model-to-dict conversion without the overhead of schema libraries like marshmallow. It supports custom serialization logic per column, timezone handling via pytz, recursion depth limits to prevent infinite loops, and the ability to include non-SQLAlchemy fields or method results in the output. Runtime dependencies are SQLAlchemy, psycopg2-binary, pytz, and setuptools.

Use it for:

  • Convert SQLAlchemy model instances to dictionaries for JSON API responses without defining separate serialization schemas.
  • Exclude sensitive fields (passwords, tokens) from serialized output using negative rules or custom serializers.
  • Serialize nested relationships and foreign-key references in a single call with configurable depth limits.
  • Add computed or non-database fields to serialized output by including methods or properties in the rules.
  • Filter out null or empty values from the serialized dictionary to reduce payload size in API responses.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Adds a `.to_dict()` method to SQLAlchemy model instances, converting them to dictionaries with support for nested relationships, custom field rules, and timezone handling.

Yes. The package is actively maintained, has no known vulnerabilities, uses a permissive MIT license, and solves a common problem—model serialization—with minimal dependencies and low install friction. It's well-suited for Flask/FastAPI applications and other projects where lightweight, ad-hoc model-to-dict conversion is needed. Install it if you want to avoid the boilerplate of schema libraries for straightforward serialization tasks.

Install

sqlalchemy-serializer on PyPI

pip

pip install sqlalchemy-serializer

uv

uv add sqlalchemy-serializer

poetry

poetry add sqlalchemy-serializer

Installing sqlalchemy-serializer

Before you install

Low friction: pure Python wheel with four runtime dependencies (SQLAlchemy, psycopg2-binary, pytz, setuptools). Actively maintained with recent releases; last commit 2026-05-11 and 201 repository stars indicate steady upkeep.

License in practice

MIT license (permissive) allows use in commercial and proprietary projects with minimal restrictions—include a copy of the license and attribute the original author.

Quickstart

from sqlalchemy_serializer import SerializerMixin

class MyModel(db.Model, SerializerMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(256))

item = MyModel.query.first()
result = item.to_dict()

Requires Python 3.10 or later.

Verify before relying

  • Whether psycopg2-binary is required for all use cases or only when using PostgreSQL backends.
  • Performance characteristics when serializing deeply nested relationships or large model graphs.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 4 — SQLAlchemy, psycopg2-binary, pytz, setuptools
Maintenance actively maintained — 118 days since the last release
Last repo commit
First released
Downloads 163,975/month — #10,558 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: sqlalchemy_serializer-1.6.3-py3-none-any.whl

Keywords: sqlalchemy, serialize, to_dict, JSON

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

sqlalchemy model to dictserialize sqlalchemy objectssqlalchemy to jsonsqlalchemy model serializationconvert sqlalchemy to dictionarysqlalchemy mixin serializersqlalchemy json export
orm-serializationjson-exportrest-api

More Database packages

psycopg2-binary

psycopg2-binary is a PostgreSQL database…

copyleft · top 1,000 on PyPI

redis

Python client library for connecting to and…

permissive · top 1,000 on PyPI

ydb

YDB Python SDK is the official client library…

permissive · top 1,000 on PyPI

snowflake-connector-python

Connects Python applications to Snowflake data…

permissive · top 1,000 on PyPI

sqlparse

sqlparse tokenizes SQL text into a tree of…

permissive · top 1,000 on PyPI

dbt-adapters

Provides base adapter protocols and shared…

permissive · top 1,000 on PyPI

marshmallow-sqlalchemy

Generates marshmallow schemas from SQLAlchemy…

permissive · top 5,000 on PyPI

sqlalchemy-json

Provides mutation-tracked JSON column types for…

permissive · top 5,000 on PyPI

sqlalchemy-easy-softdelete

Adds soft-deletion support to SQLAlchemy models…

permissive · top 15,000 on PyPI

sqlalchemy-mixins

Provides Active Record-style mixins for…

permissive · top 15,000 on PyPI

drf-writable-nested

Extends Django REST Framework serializers to…

permissive · top 15,000 on PyPI

voluptuous-serialize

Converts Voluptuous schema objects into…

permissive · top 15,000 on PyPI

marshmallow-jsonapi

Produces JSON API 1.0-compliant output from…

permissive · top 15,000 on PyPI

sqlalchemy-repr

Adds automatic, human-readable repr() output to…

unclear · top 15,000 on PyPI

sqlacodegen

Reads an existing database schema and generates…

permissive · top 5,000 on PyPI

drf-jsonschema-serializer

Converts Django REST Framework serializers to…

permissive · top 15,000 on PyPI