Django like model serializer
Project description
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 |
|
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
File details
Details for the file alcherializer-0.5.0.tar.gz
.
File metadata
- Download URL: alcherializer-0.5.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb0f72657a0c9fc634b06ab8edc5d3f6f1ac158b431a73085c30c7d52b534a65 |
|
MD5 | 99dd539c7053515fd405f4a32a4f178b |
|
BLAKE2b-256 | 4c2e8501bc8759c9c7248c82334e4e5a435fcbca0ce45ae1c1a3fa6e176f876d |
File details
Details for the file alcherializer-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: alcherializer-0.5.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81291ee9f52414abad8514c3ef7fd26f78e0052c3c804a0b53a223950ab6d697 |
|
MD5 | cacaf532677cc23f074cce16efdd04fc |
|
BLAKE2b-256 | 7fece4a0796065c431c09c8c47280042bacc8acc6a6a4c2ddf7c111d8856b2ce |