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
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)
class UserSerializer(Serializer):
class Meta:
model = User
PS: For further exemplifications we will always use User and UserSerializer.
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"]}
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
alcherializer-0.1.0.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for alcherializer-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e8c6e0834d78f2aa114294983afbd11a0665570cf93adb143f184b62c10c809 |
|
MD5 | 347ea80ef5e53218d99d7567931d2964 |
|
BLAKE2b-256 | 0ff2af5b3302f27fe0fc07d53d41f669674b024d0adc038bb4fcecaaeae08ad0 |