Serializers for SQLAlchemy models.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file serialchemy-1.0.3.tar.gz.
File metadata
- Download URL: serialchemy-1.0.3.tar.gz
- Upload date:
- Size: 57.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66c5a715c0da19372a016ea84fdbd06799b1eed87904a668d6d554507d399a9f
|
|
| MD5 |
c8525b093323e820e21f2b48ffd2f19a
|
|
| BLAKE2b-256 |
600c8cc4aff867a2cb62145256ecae5c33ca44749c06b8ec72a9ab9967d619d4
|
Provenance
The following attestation bundles were made for serialchemy-1.0.3.tar.gz:
Publisher:
main.yml on ESSS/serialchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
serialchemy-1.0.3.tar.gz -
Subject digest:
66c5a715c0da19372a016ea84fdbd06799b1eed87904a668d6d554507d399a9f - Sigstore transparency entry: 626728204
- Sigstore integration time:
-
Permalink:
ESSS/serialchemy@ae59b3842a2b64d3f834823e92b3884335476fff -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/ESSS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@ae59b3842a2b64d3f834823e92b3884335476fff -
Trigger Event:
push
-
Statement type:
File details
Details for the file serialchemy-1.0.3-py3-none-any.whl.
File metadata
- Download URL: serialchemy-1.0.3-py3-none-any.whl
- Upload date:
- Size: 35.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe8207567b94ee09af592b950b21517b73e1432bb1dced5f0dc2844f4ebfbc6e
|
|
| MD5 |
ca3ea2afd3454b2cc307e12cb842045b
|
|
| BLAKE2b-256 |
915b1137f9cf74a99653a5263a15092f733a010d9d62baa41e7bf893eeed541f
|
Provenance
The following attestation bundles were made for serialchemy-1.0.3-py3-none-any.whl:
Publisher:
main.yml on ESSS/serialchemy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
serialchemy-1.0.3-py3-none-any.whl -
Subject digest:
fe8207567b94ee09af592b950b21517b73e1432bb1dced5f0dc2844f4ebfbc6e - Sigstore transparency entry: 626728212
- Sigstore integration time:
-
Permalink:
ESSS/serialchemy@ae59b3842a2b64d3f834823e92b3884335476fff -
Branch / Tag:
refs/tags/v1.0.3 - Owner: https://github.com/ESSS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@ae59b3842a2b64d3f834823e92b3884335476fff -
Trigger Event:
push
-
Statement type: