Skip to main content

Serialchemy

https://img.shields.io/pypi/v/serialchemy.svg https://img.shields.io/pypi/pyversions/serialchemy.svg https://github.com/ESSS/serialchemy/workflows/build/badge.svg https://codecov.io/gh/ESSS/serialchemy/branch/master/graph/badge.svg https://img.shields.io/readthedocs/serialchemy.svg https://sonarcloud.io/api/project_badges/measure?project=ESSS_serialchemy&metric=alert_status

SQLAlchemy model serialization.

Motivation

Serialchemy was developed as a module of Flask-RESTAlchemy, a lib to create Restful APIs using Flask and SQLAlchemy. We first tried marshmallow-sqlalchemy, probably the most well-known lib for SQLAlchemy model serialization, but we faced issues related to nested models. We also think that is possible to build a simpler and more maintainable solution by having SQLAlchemy in mind from the ground up, as opposed to marshmallow-sqlalchemy that had to be designed and built on top of marshmallow.

How to Use it

Serializing Generic Types

Suppose we have an Employee SQLAlchemy model declared:

class Employee(Base):
    __tablename__ = "Employee"

    id = Column(Integer, primary_key=True)
    fullname = Column(String)
    admission = Column(DateTime, default=datetime(2000, 1, 1))
    company_id = Column(ForeignKey("Company.id"))
    company = relationship(Company)
    company_name = column_property(
        select([Company.name]).where(Company.id == company_id)
    )
    password = Column(String)

Generic Types are automatically serialized by ModelSerializer:

from serialchemy import ModelSerializer

emp = Employee(fullname="Roberto Silva", admission=datetime(2019, 4, 2))

serializer = ModelSerializer(Employee)
serializer.dump(emp)

# >>
{
    "id": None,
    "fullname": "Roberto Silva",
    "admission": "2019-04-02T00:00:00",
    "company_id": None,
    "company_name": None,
    "password": None,
}

New items can be deserialized by the same serializer:

new_employee = {"fullname": "Jobson Gomes", "admission": "2018-02-03"}
serializer.load(new_employee)

# >> <Employee object at 0x000001C119DE3940>

Serializers do not commit into the database. You must do this by yourself:

emp = serializer.load(new_employee)
session.add(emp)
session.commit()

Custom Serializers

For anything beyond Generic Types we must extend the ModelSerializer class:

class EmployeeSerializer(ModelSerializer):

    password = Field(load_only=True)  # passwords should be only deserialized
    company = NestedModelField(Company)  # dump company as nested object


serializer = EmployeeSerializer(Employee)
serializer.dump(emp)
# >>
{
    "id": 1,
    "fullname": "Roberto Silva",
    "admission": "2019-04-02T00:00:00",
    "company": {"id": 3, "name": "Acme Co"},
}
Extend Polymorphic Serializer

One of the possibilities is to serialize SQLalchemy joined table inheritance and it child tables as well. To do such it’s necessary to set a variable with the desired model class name. Take this Employee class with for instance and let us assume it have a joined table inheritance:

class Employee(Base):
    ...
    type = Column(String(50))

    __mapper_args__ = {"polymorphic_identity": "employee", "polymorphic_on": type}


class Engineer(Employee):
    __tablename__ = "Engineer"
    id = Column(Integer, ForeignKey("employee.id"), primary_key=True)
    association = relationship(Association)

    __mapper_args__ = {
        "polymorphic_identity": "engineer",
    }

To use a extended ModelSerializer class on the Engineer class, you should create the serializer as it follows:

class EmployeeSerializer(
    PolymorphicModelSerializer
):  # Since this class will be polymorphic

    password = Field(load_only=True)
    company = NestedModelField(Company)


class EngineerSerializer(EmployeeSerializer):
    __model_class__ = Engineer  # This is the table Serialchemy will refer to
    association = NestedModelField(Association)

Contributing

For guidance on setting up a development environment and how to make a contribution to serialchemy, see the contributing guidelines.

Release

A reminder for the maintainers on how to make a new release.

Note that the VERSION should folow the semantic versioning as X.Y.Z Ex.: v1.0.5

Create a release-VERSION branch from upstream/master. Update CHANGELOG.rst. Push a branch with the changes. Once all builds pass, push a VERSION tag to upstream. Ex: git tag v1.0.5; git push origin –tags Merge the PR.

Metadata

Release files for serialchemy 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for serialchemy 1.0.3
File Size Uploaded
serialchemy-1.0.3.tar.gz 57.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for serialchemy 1.0.3
File Interpreter ABI Platform
serialchemy-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 92.5 kB

Release files / serialchemy-1.0.3.tar.gz

Download URL serialchemy-1.0.3.tar.gz
Size 57.0 kB
Tags Source
SHA-256 checksum
How to use checksums
66c5a715c0da19372a016ea84fdbd06799b1eed87904a668d6d554507d399a9f
BLAKE2b-256 checksum
How to use checksums
600c8cc4aff867a2cb62145256ecae5c33ca44749c06b8ec72a9ab9967d619d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2025.

Transparency log

Release files / serialchemy-1.0.3-py3-none-any.whl

Download URL serialchemy-1.0.3-py3-none-any.whl
Size 35.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe8207567b94ee09af592b950b21517b73e1432bb1dced5f0dc2844f4ebfbc6e
BLAKE2b-256 checksum
How to use checksums
915b1137f9cf74a99653a5263a15092f733a010d9d62baa41e7bf893eeed541f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2025.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page