Skip to content

alexopryshko/django-serializer

Repository files navigation

Django-serializer

Installation

django-serializer packaged and delivered via pypi

pip install django-serializer

Features

Serialization provided by marshmallow. The library provide generic and custom CRUD views and paginators. Automatic swagger generation provided by apispec

Changelog

See HISTORY.md

Quickstart

basic flow

from django_serializer.v2.serializer import ModelSerializer
from django_serializer.v2.views import ApiView 
from myapp.models import MyModel

class MyModelSerializer(ModelSerializer):
    class SMeta:
        model = MyModel
        fields = ('id', 'foo', 'bar')

class MyView(ApiView):
    class Meta:
        serializer = MyModelSerializer

    def execute(self, request, *args, **kwargs):
        return MyModel.objects.first()

multiple objects

from django_serializer.v2.serializer import ModelSerializer
from django_serializer.v2.views import ApiView 
from myapp.models import MyModel

class MyModelSerializer(ModelSerializer):
    class SMeta:
        model = MyModel
        fields = ('id', 'foo', 'bar')

class MyListView(ApiView):
    class Meta:
        serializer = MyModelSerializer
        serializer_many = True

    def execute(self, request, *args, **kwargs):
        return MyModel.objects.first()

using query params

from django import forms
from django_serializer.v2.views import ApiView 

class MyQueryForm(forms.Form):
    id = forms.IntegerField() 

class MyListView(ApiView):
    class Meta:
        query_form = MyQueryForm
        serializer = MyModelSerializer
        serializer_many = True
        
    def execute(self, request, *args, **kwargs):
        object_id = self.request_query['id']
        return MyModel.objects.filter(id=object_id).first()

add swagger

# urls.py
from django_serializer.v2.swagger.views import index
from django.urls import path

urlpatterns = [
    path('swagger.json', index),
]

Contribution

Feel free to open pull request to the latest rc-<version> branch.

Licence

See LICENSE