A library for handling common stuff in Python
Project description
drf-common
Installation
pip install commonkit
Examples
# models.py
from django.db import models
class Human(models.Model):
class Level(models.IntegerChoices):
BEGINNER = 0
INTERMEDIATE = 1
ADVANCED = 2
class MilitaryStatus(models.TextChoices):
EXEMPTED = "exempted", "Exempted"
SERVED = "served", "Served"
POSTPONED = "postponed", "Postponed"
name = models.CharField(max_length=128)
level = models.IntegerField(choices=Level.choices)
military_status = models.CharField(
choices=MilitaryStatus.choices,
max_length=128
)
# serializers.py
from rest_framework import serializers
from commonkit.rest_framework.serializers import EnumSerializer
from example.core import models
class HumanSerializer(serializers.ModelSerializer):
level = EnumSerializer(enum=models.Human.Level)
military_status = EnumSerializer(enum=models.Human.MilitaryStatus)
class Meta:
model = models.Human
fields = [
'id',
'level',
'military_status',
]
Example serialization
# wherever.py
from example.core.serializers import HumanSerializer
from example.core.models import Human
human = Human.objects.find(id=1)
serializer = HumanSerializer(human) # level = BEGINNER, military_status = EXEMPTED
Example deserialization
# wherever.py
from example.core.serializers import HumanSerializer
data = {
"level": "BEGINNER",
"MILITARY_STATUS": "EXEMPTED",
}
instance = HumanSerializer(data=data)
instance.is_valid()
instance.save()
To run the project
pip install -r requirements.txt
pre-commit install
To build the docs
make html
To run the tests
pytest .
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
commonkit-1.0.7.tar.gz
(10.4 kB
view hashes)
Built Distribution
commonkit-1.0.7-py3-none-any.whl
(13.2 kB
view hashes)
Close
Hashes for commonkit-1.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ef95bb546c37c59c781e26b7cace3889c2873147c3a31edac83f47957c81881 |
|
MD5 | 3a963cae80e7af90335cbb96cb5f389d |
|
BLAKE2b-256 | 9d3bb16414126c47bb5b0ade62c7e3b126f4bcd27d856d27b4b746017887133a |