[![Github Actions](https://github.com/vinyguedess/alcherializer/actions/workflows/main.yml/badge.svg)](https://github.com/vinyguedess/alcherializer/actions/workflows/main.yml)
[![Maintainability](https://api.codeclimate.com/v1/badges/332cfdc498df9f6dc272/maintainability)](https://codeclimate.com/github/vinyguedess/alcherializer/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/332cfdc498df9f6dc272/test_coverage)](https://codeclimate.com/github/vinyguedess/alcherializer/test_coverage)
# 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.
```python
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.
```python
model = User(
name="Clark Kent",
age=31,
is_active=True)
serializer = UserSerializer(model)
serializer.data # { "name": "Clark Kent", ... }
```
Or, a list of models
```python
model = User(
name="Clark Kent",
age=31,
is_active=True)
serializer = UserSerializer([model], many=True)
serializer.data # [{ "name": "Clark Kent", ... }]
```
### Related Serializers
```python
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
```python
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.
```python
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.
```python
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 | <ul><li>[x] Required</li><li>[x] Valid boolean</li></ul>
BigInteger, Integer, SmallInteger | IntegerField | <ul><li>[x] Required</li></ul>
String, Text Unicode | StringField | <ul><li>[x] Required</li><li>[x] Max length</li></ul>
Raw data
{
"_id": null,
"home_page": "https://github.com/vinyguedess/alcherializer",
"name": "alcherializer",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "django,flask,serializer,sql,sqlalchemy,alchemy",
"author": "Vinicius Guedes",
"author_email": "viniciusgued@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/4c/2e/8501bc8759c9c7248c82334e4e5a435fcbca0ce45ae1c1a3fa6e176f876d/alcherializer-0.5.0.tar.gz",
"platform": null,
"description": "[![Github Actions](https://github.com/vinyguedess/alcherializer/actions/workflows/main.yml/badge.svg)](https://github.com/vinyguedess/alcherializer/actions/workflows/main.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/332cfdc498df9f6dc272/maintainability)](https://codeclimate.com/github/vinyguedess/alcherializer/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/332cfdc498df9f6dc272/test_coverage)](https://codeclimate.com/github/vinyguedess/alcherializer/test_coverage)\n\n# Alcherializer\nA \"Django like\" model serializer.\n\n## Declaring Serializer\nIt's very simples to declare a serializer. Just like Django, the only\nthing you need is to create a class with a Meta class inside and\na model attribute.\n\nThis instantly maps all fields declared in model.\n```python\nfrom datetime import datetime\nfrom alcherializer import Serializer\nimport sqlalchemy\nfrom sqlalchemy.orm import relationship\n\n\nclass Manager:\n name = sqlalchemy.Column(sqlalchemy.String(100))\n\n\nclass User:\n name = sqlalchemy.Column(sqlalchemy.String(100))\n age = sqlalchemy.Column(sqlalchemy.Integer)\n is_active = sqlalchemy.Column(sqlalchemy.Boolean)\n created_at = sqlalchemy.Column(sqlalchemy.DateTime, default=datetime.utcnow)\n\n organization = relationship(Manager, uselist=False)\n\n\nclass UserSerializer(Serializer):\n class Meta:\n model = User\n```\nPS: For further exemplifications we will always use **User** and **UserSerializer**.\n\n## Data\nGets a dictionary of a single model.\n```python\nmodel = User(\n name=\"Clark Kent\",\n age=31,\n is_active=True)\n\nserializer = UserSerializer(model)\nserializer.data # { \"name\": \"Clark Kent\", ... }\n```\n\nOr, a list of models\n```python\nmodel = User(\n name=\"Clark Kent\",\n age=31,\n is_active=True)\n\nserializer = UserSerializer([model], many=True)\nserializer.data # [{ \"name\": \"Clark Kent\", ... }]\n```\n\n### Related Serializers\n```python\nclass ManagerSerializer(Serializer):\n class Meta:\n model = Manager\n\n\nclass UserSerializer(Serializer):\n manager = ManagerSerializer()\n\n class Meta:\n model = User\n\n\nmodel = User(\n name=\"Peter Parker\",\n manager=Manager(\n name=\"J. Jonah Jameson\"\n )\n)\nserializer = UserSerializer(model)\nserializer.data # {\"name\": \"Peter Parker\", \"manager\": {\"name\": \"J. Jonah Jameson\"}}\n```\n\n### Custom fields\n```python\nfrom datetime import datetime, timedelta\nfrom alcherializer import fields\n\nclass UserSerializer(Serializer):\n year_of_birth = fields.MethodField()\n\n def get_year_of_birth(self, user: User) -> datetime:\n return datetime.utcnow() - timedelta(days=user.age * 365)\n\n class Meta:\n model = User\n fields = [\"id\", \"name\", \"year_of_birth\"]\n\n\nmodel = User(id=1, name=\"Batman\", age=30)\n\nserializer = UserSerializer(model)\nserializer.data # {\"id\": 1, \"name\": \"Batman\", \"year_of_birth\": 1991}\n```\n\n## Validation\nTo validate a payload, it's possible to send it through data argument while\ninstantiating the serializer and call **.is_valid** method.\n```python\nserializer = UserSerializer(data={\n \"name\": \"Clark Kent\",\n \"age\": 31,\n \"is_active\": True\n})\nserializer.is_valid() # True\n```\n\n### Fetching validation errors\nIf any error happens you can fetch the information through error attribute.\n```python\nserializer = UserSerializer(data={\n \"name\": \"\", # If ommitted or None should present error too\n \"age\": 31,\n \"is_active\": True\n})\nserializer.is_valid() # False\nserializer.errors # {\"name\": [\"Can't be blank\"]}\n```\n\n## Fields\nThis shows off how fields are mapped from SQLAlchemy models.\n\nModel attribute | Alcherializer field | Validations\n--- | --- |---\nBoolean | BooleanField | <ul><li>[x] Required</li><li>[x] Valid boolean</li></ul>\nBigInteger, Integer, SmallInteger | IntegerField | <ul><li>[x] Required</li></ul>\nString, Text Unicode | StringField | <ul><li>[x] Required</li><li>[x] Max length</li></ul>\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django like model serializer",
"version": "0.5.0",
"split_keywords": [
"django",
"flask",
"serializer",
"sql",
"sqlalchemy",
"alchemy"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "cacaf532677cc23f074cce16efdd04fc",
"sha256": "81291ee9f52414abad8514c3ef7fd26f78e0052c3c804a0b53a223950ab6d697"
},
"downloads": -1,
"filename": "alcherializer-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cacaf532677cc23f074cce16efdd04fc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5363,
"upload_time": "2022-12-20T15:31:58",
"upload_time_iso_8601": "2022-12-20T15:31:58.630368Z",
"url": "https://files.pythonhosted.org/packages/7f/ec/e4a0796065c431c09c8c47280042bacc8acc6a6a4c2ddf7c111d8856b2ce/alcherializer-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "99dd539c7053515fd405f4a32a4f178b",
"sha256": "bb0f72657a0c9fc634b06ab8edc5d3f6f1ac158b431a73085c30c7d52b534a65"
},
"downloads": -1,
"filename": "alcherializer-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "99dd539c7053515fd405f4a32a4f178b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4896,
"upload_time": "2022-12-20T15:32:00",
"upload_time_iso_8601": "2022-12-20T15:32:00.598543Z",
"url": "https://files.pythonhosted.org/packages/4c/2e/8501bc8759c9c7248c82334e4e5a435fcbca0ce45ae1c1a3fa6e176f876d/alcherializer-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-20 15:32:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "vinyguedess",
"github_project": "alcherializer",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "SQLAlchemy",
"specs": [
[
">=",
"1.3.20"
],
[
"<=",
"1.4.18"
]
]
}
],
"lcname": "alcherializer"
}