Alcherializer
A "Django like" model serializer.
Declaring Serializer
It's very simples to declare a serializer. Just like Django, the only thing you need is to create a class with a Meta class inside and a model attribute.
This instantly maps all fields declared in model.
from datetime import datetime
from alcherializer import Serializer
import sqlalchemy
from sqlalchemy.orm import relationship
class Manager:
name = sqlalchemy.Column(sqlalchemy.String(100))
class User:
name = sqlalchemy.Column(sqlalchemy.String(100))
age = sqlalchemy.Column(sqlalchemy.Integer)
is_active = sqlalchemy.Column(sqlalchemy.Boolean)
created_at = sqlalchemy.Column(sqlalchemy.DateTime, default=datetime.utcnow)
organization = relationship(Manager, uselist=False)
class UserSerializer(Serializer):
class Meta:
model = User
PS: For further exemplifications we will always use User and UserSerializer.
Data
Gets a dictionary of a single model.
model = User(
name="Clark Kent",
age=31,
is_active=True)
serializer = UserSerializer(model)
serializer.data # { "name": "Clark Kent", ... }
Or, a list of models
model = User(
name="Clark Kent",
age=31,
is_active=True)
serializer = UserSerializer([model], many=True)
serializer.data # [{ "name": "Clark Kent", ... }]
Related Serializers
class ManagerSerializer(Serializer):
class Meta:
model = Manager
class UserSerializer(Serializer):
manager = ManagerSerializer()
class Meta:
model = User
model = User(
name="Peter Parker",
manager=Manager(
name="J. Jonah Jameson"
)
)
serializer = UserSerializer(model)
serializer.data # {"name": "Peter Parker", "manager": {"name": "J. Jonah Jameson"}}
Custom fields
from datetime import datetime, timedelta
from alcherializer import fields
class UserSerializer(Serializer):
year_of_birth = fields.MethodField()
def get_year_of_birth(self, user: User) -> datetime:
return datetime.utcnow() - timedelta(days=user.age * 365)
class Meta:
model = User
fields = ["id", "name", "year_of_birth"]
model = User(id=1, name="Batman", age=30)
serializer = UserSerializer(model)
serializer.data # {"id": 1, "name": "Batman", "year_of_birth": 1991}
Validation
To validate a payload, it's possible to send it through data argument while instantiating the serializer and call .is_valid method.
serializer = UserSerializer(data={
"name": "Clark Kent",
"age": 31,
"is_active": True
})
serializer.is_valid() # True
Fetching validation errors
If any error happens you can fetch the information through error attribute.
serializer = UserSerializer(data={
"name": "", # If ommitted or None should present error too
"age": 31,
"is_active": True
})
serializer.is_valid() # False
serializer.errors # {"name": ["Can't be blank"]}
Fields
This shows off how fields are mapped from SQLAlchemy models.
| Model attribute | Alcherializer field | Validations |
|---|---|---|
| Boolean | BooleanField |
|
| BigInteger, Integer, SmallInteger | IntegerField |
|
| String, Text Unicode | StringField |
|
Metadata
Release files for alcherializer 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| alcherializer-0.5.0.tar.gz | 4.9 kB | Without provenance | Details for alcherializer-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| alcherializer-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for alcherializer-0.5.0-py3-none-any.whl |
Total release size: 10.3 kB
Release files / alcherializer-0.5.0.tar.gz
| Download URL | alcherializer-0.5.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bb0f72657a0c9fc634b06ab8edc5d3f6f1ac158b431a73085c30c7d52b534a65
|
|
BLAKE2b-256 checksum How to use checksums |
4c2e8501bc8759c9c7248c82334e4e5a435fcbca0ce45ae1c1a3fa6e176f876d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.15
|
Release files / alcherializer-0.5.0-py3-none-any.whl
| Download URL | alcherializer-0.5.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
81291ee9f52414abad8514c3ef7fd26f78e0052c3c804a0b53a223950ab6d697
|
|
BLAKE2b-256 checksum How to use checksums |
7fece4a0796065c431c09c8c47280042bacc8acc6a6a4c2ddf7c111d8856b2ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.15
|
Security for alcherializer 0.5.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.