Skip to main content

Serializers for SQLAlchemy models.

Project description

Serialchemy

https://img.shields.io/travis/ESSS/serialchemy.svg https://ci.appveyor.com/api/projects/status/github/ESSS/serialchemy?branch=master https://codecov.io/gh/ESSS/serialchemy/branch/master/graph/badge.svg https://img.shields.io/readthedocs/pip.svg

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'
               }
    }

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

  1. Create a release-VERSION branch from upstream/master.

  2. Update CHANGELOG.rst.

  3. Push a branch with the changes.

  4. Once all builds pass, push a VERSION tag to upstream.

  5. Merge the PR.

History

0.3.0 (2019-17-07)

  • Add the composite fields to list of properties of model, to serialize that fields if it type is in EXTRA_SERIALIZERS.

  • Fix error for SQLAlchemy composite attributes

  • Added free functions dump and load so users can quickly dump a SQLAlchemy model without having to instancialize ModelSerializer.

0.2.0 (2019-03-22)

  • Fix: Error when deserializing of nested models when SQLAlchemy model primary key attribute name differs from the column name

  • Allow EXTRA_SERIALIZERS to be defined in runtime

  • Check if a session was given when serializing/deserializing nested fields

0.1.0 (2019-02-12)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serialchemy-0.20.0rc6.post13.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serialchemy-0.20.0rc6.post13-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file serialchemy-0.20.0rc6.post13.tar.gz.

File metadata

  • Download URL: serialchemy-0.20.0rc6.post13.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for serialchemy-0.20.0rc6.post13.tar.gz
Algorithm Hash digest
SHA256 d2dd312b493d593d75df10a7726cb4ec00cfea03031619ea2048ef1fe4aa8329
MD5 afa9c861305e44dfacf27ad6ce6ae9ba
BLAKE2b-256 3a0b34525307378a594c095f2ab62c6057876b1ec2aed6da004d96905ce80a85

See more details on using hashes here.

File details

Details for the file serialchemy-0.20.0rc6.post13-py2.py3-none-any.whl.

File metadata

  • Download URL: serialchemy-0.20.0rc6.post13-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7

File hashes

Hashes for serialchemy-0.20.0rc6.post13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1e5f40edd419b32a6bf5c5ac5b1bb12069d1c4ed7535192b7c880132e1d450e7
MD5 12b1bbc07d1dcc9c64d8a971e52a2f3d
BLAKE2b-256 6e41e14dec138cf1c2f64704e314d69570d58f55872305ffca54562f3d4f011f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page