All Projects → Mng-dev-ai → drf-turbo

Mng-dev-ai / drf-turbo

Licence: MIT license
An alternative serializer implementation for REST framework written in cython built for speed.

Programming Languages

python
139335 projects - #7 most used programming language
cython
566 projects
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to drf-turbo

drf-registration
Simple user registration package based on Django Rest Framework. DRF Registration - The easy way to generate registration RESTful APIs
Stars: ✭ 32 (-56.16%)
Mutual labels:  django-rest-framework
machado
This repository provides users with a framework to store, search and visualize biological data.
Stars: ✭ 18 (-75.34%)
Mutual labels:  django-rest-framework
school-navigator
Navigate the Durham, NC public school system
Stars: ✭ 25 (-65.75%)
Mutual labels:  django-rest-framework
django-learning-pathway
(Currently in development) Learning pathways for learning Django.
Stars: ✭ 35 (-52.05%)
Mutual labels:  django-rest-framework
filtermapbackend
FilterMapBackend for django-rest-framework
Stars: ✭ 16 (-78.08%)
Mutual labels:  django-rest-framework
drf ujson2
JSON parser and renderer using ujson for Django Rest Framework
Stars: ✭ 29 (-60.27%)
Mutual labels:  django-rest-framework
Footprint
Bluetooth Beacon 을 활용한 장소 기반 추억 기록 및 공유 서비스
Stars: ✭ 24 (-67.12%)
Mutual labels:  django-rest-framework
drf tweaks
Extensions for Django Rest Framework
Stars: ✭ 77 (+5.48%)
Mutual labels:  django-rest-framework
Auto-DL
Auto-DL helps you make Deep Learning models without writing a single line of code and giving as little input as possible.
Stars: ✭ 165 (+126.03%)
Mutual labels:  django-rest-framework
python-web-dev-21-2
Material for "Web Development in Python with Django" using Django 2.1, published as a Pearson LiveLesson on Safari Books Online
Stars: ✭ 38 (-47.95%)
Mutual labels:  django-rest-framework
Malicious-Urlv5
A multi-layered and multi-tiered Machine Learning security solution, it supports always on detection system, Django REST framework used, equipped with a web-browser extension that uses a REST API call.
Stars: ✭ 35 (-52.05%)
Mutual labels:  django-rest-framework
DjangoUnboxed
Production ready django based starter kit
Stars: ✭ 67 (-8.22%)
Mutual labels:  django-rest-framework
ai web RISKOUT BTS
국방 리스크 관리 플랫폼 (🏅 국방부장관상/Minister of National Defense Award)
Stars: ✭ 18 (-75.34%)
Mutual labels:  django-rest-framework
drf-action-serializer
A serializer for the Django Rest Framework that supports per-action serialization of fields.
Stars: ✭ 48 (-34.25%)
Mutual labels:  django-rest-framework
django-rest-framework-example
REST framework example for Django < 1.9 // New version ->
Stars: ✭ 29 (-60.27%)
Mutual labels:  django-rest-framework
Deep-learning-model-deploy-with-django
Serving a keras model (neural networks) in a website with the python Django-REST framework.
Stars: ✭ 76 (+4.11%)
Mutual labels:  django-rest-framework
csf
ArmourBird CSF - Container Security Framework
Stars: ✭ 48 (-34.25%)
Mutual labels:  django-rest-framework
dr scaffold
scaffold django rest apis like a champion 🚀
Stars: ✭ 116 (+58.9%)
Mutual labels:  django-rest-framework
repanier
Django extension : web tool for short circuit food supply
Stars: ✭ 18 (-75.34%)
Mutual labels:  django-rest-framework
django-rest-framework-datatables-editor
Seamless integration between Django REST framework, Datatables and Datatables Editor.
Stars: ✭ 25 (-65.75%)
Mutual labels:  django-rest-framework

drf-turbo

Version Documentation Status Downloads

Overview

drf-turbo is a drop-in serializer for Django REST Framework (DRF). drf-turbo serializers run around 7.75 times faster than what what you get from DRF's packaged serializer.

NOTE: It is written in Cython, which is required to build this package.

Requirements

  • Django
  • Django REST Framework
  • Cython
  • forbiddenfruit
  • pyyaml(OpenAPI)
  • uritemplate(OpenAPI)
  • djangorestframework-simplejwt(OpenAPI)

Installation

$ pip install drf-turbo

To install Cython on MacOS via Brew:

$ brew install cython

Performance

drf-turbo serialization, deserialization and validation performance averages 86% faster than DRF's standard serializer.

For more details, visit the benchmarks section of the docs.

Documentation & Support

Documentation for the project is available at https://drf-turbo.readthedocs.io.

For questions and support, use github issues

Examples

Declaring Serializers

from datetime import datetime
from django.utils.timezone import now
import drf_turbo as dt

 class User:
     def __init__(self, username, email,created=None):
         self.username = username
         self.email = email
         self.created = created or datetime.now()

 user = User(username='test' , email='[email protected]')



 class UserSerializer(dt.Serializer):
     username = dt.StrField(max_length=50)
     email = dt.EmailField()
     created = dt.DateTimeField()

Serializing objects

serializer = UserSerializer(user)
serializer.data

 # {'username': 'test', 'email': '[email protected]', 'created': '2021-11-04T22:49:01.981127Z'}

Deserializing objects

data = {'username':'new_test','email':'[email protected]','created':now()}
serializer = UserSerializer(data=data)
serializer.is_valid()
# True
serializer.validated_data
# {'username': 'new_test', 'email': '[email protected]', 'created': datetime.datetime(2021, 11, 12, 6, 10, 44, 85118)}}

Validation

serializer = UserSerializer(data={'email': 'test'})
serializer.is_valid()
# False
serializer.errors
# {'username': ['This field is required.'], 'email': ['Enter a valid email address.'],'created': ['This field is required.']}

Field-level validation

import drf_turbo as dt

class UserSerializer(dt.Serializer):
    username = dt.StrField(max_length=50)

    def validate_username(self, value):
        if 'test' not in value.lower():
            raise dt.ValidationError("test must be in username")
        return value

Object-level validation

import drf_turbo as dt

class CampaignSerializer(dt.Serializer):
    start_date = dt.DateTimeField()
    end_date = dt.DateTimeField()

    def validate(self, data):
        if data['start_date'] > data['end_date']:
            raise dt.ValidationError("start_date must occur before end_date")
        return data

Nested Serializers

from datetime import datetime
from django.utils.timezone import now
import drf_turbo as dt

 class User:
     def __init__(self, username, email,created=None):
         self.username = username
         self.email = email
         self.created = created or datetime.now()

 user = User(username='test' , email='[email protected]')

 class UserSerializer(dt.Serializer):
     username = dt.StrField(max_length=50)
     email = dt.EmailField()
     created = dt.DateTimeField()

 class Profile :
     def __init__(self, age=25):
         self.age = age
         self.user = user

 profile = Profile()


 class ProfileSerializer(dt.Serializer):
     age = dt.IntField()
     user = UserSerializer()


 serializer = ProfileSerializer(profile)
 serializer.data

 # {'age' : 25 , 'user' : {'username': 'test', 'email': '[email protected]', 'created': '2021-11-04T22:49:01.981127Z'}}

Filtering Output

drf-turbo provides option to enclude or exclude fields from serializer using only or exclude keywords.

serializer = UserSerializer(user,only=('id','username'))

or

serializer = ProfileSerializer(profile,exclude=('id','user__email'))

or

http://127.0.0.1:8000/user/?only=id,username

Required Fields

Make a field required by passing required=True. An error will be raised if the the value is missing from data during Deserializing.

For example:

class UserSerializer(dt.Serializer):

    username = dt.StrField(required=True,error_messages={"required":"no username"})

Specifying Defaults

It will be used for the field if no input value is supplied.

For example:

from datetime import datetime

class UserSerializer(dt.Serializer):

    birthdate = dt.DateTimeField(default=datetime(2021, 11, 05))

ModelSerializer

Mapping serializer to Django model definitions.

Features :

  • It will automatically generate a set of fields for you, based on the model.
  • It will automatically generate validators for the serializer.
  • It includes simple default implementations of .create() and .update().
class UserSerializer(dt.ModelSerializer):

    class Meta :
        model = User
        fields = ('id','username','email')

You can also set the fields attribute to the special value __all__ to indicate that all fields in the model should be used.

For example:

class UserSerializer(dt.ModelSerializer):

    class Meta :
        model = User
        fields = '__all__'

You can set the exclude attribute to a list of fields to be excluded from the serializer.

For example:

class UserSerializer(dt.ModelSerializer):

    class Meta :
        model = User
        exclude = ('email',)

Read&Write only fields

class UserSerializer(dt.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'password','password_confirmation')
        read_only_fields = ('username')
        write_only_fields = ('password','password_confirmation')

OpenApi(Swagger)

Add drf-turbo to installed apps in settings.py

INSTALLED_APPS = [
    # ALL YOUR APPS
    'drf_turbo',
]

and then register our openapi AutoSchema with DRF.

REST_FRAMEWORK = {
    # YOUR SETTINGS
    'DEFAULT_SCHEMA_CLASS': 'drf_turbo.openapi.AutoSchema',
}

and finally add these lines in urls.py

from django.views.generic import TemplateView
from rest_framework.schemas import get_schema_view as schema_view
from drf_turbo.openapi import SchemaGenerator

urlpatterns = [
    # YOUR PATTERNS
    path('openapi', schema_view(
        title="Your Project",
        description="API for all things …",
        version="1.0.0",
        generator_class=SchemaGenerator,
        public=True,
    ), name='openapi-schema'),
    path('docs/', TemplateView.as_view(
        template_name='docs.html',
        extra_context={'schema_url':'openapi-schema'}
    ), name='swagger-ui'),
]

Now go to http://127.0.0.1:8000/docs

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

License

  • Free software: MIT license
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].