Skip to main content

Polymorphic serializers for Django REST Framework.

Project description

https://travis-ci.org/apirobot/django-rest-polymorphic.svg?branch=master https://codecov.io/gh/apirobot/django-rest-polymorphic/branch/master/graph/badge.svg https://badge.fury.io/py/django-rest-polymorphic.svg

Django REST Polymorphic

Polymorphic serializers for Django REST Framework.

Overview

django-rest-polymorphic allows you to easily define serializers for your inherited models that you have created using django-polymorphic library.

Installation

Install using pip:

$ pip install django-rest-polymorphic

Usage

Define your polymorphic models:

# models.py
from django.db import models
from polymorphic.models import PolymorphicModel


class Project(PolymorphicModel):
    topic = models.CharField(max_length=30)


class ArtProject(Project):
    artist = models.CharField(max_length=30)


class ResearchProject(Project):
    supervisor = models.CharField(max_length=30)

Define serializers for each polymorphic model the way you did it when you used django-rest-framework:

# serializers.py
from rest_framework import serializers
from .models import Project, ArtProject, ResearchProject


class ProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Project
        fields = ('topic', )


class ArtProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = ArtProject
        fields = ('topic', 'artist')


class ResearchProjectSerializer(serializers.ModelSerializer):
    class Meta:
        model = ResearchProject
        fields = ('topic', 'supervisor')

Then you have to create a polymorphic serializer that serves as a mapper between models and serializers which you have defined above:

# serializers.py
from rest_polymorphic.serializers import PolymorphicSerializer


class ProjectPolymorphicSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        Project: ProjectSerializer,
        ArtProject: ArtProjectSerializer,
        ResearchProject: ResearchProjectSerializer
    }

Create viewset with serializer_class equals to your polymorphic serializer:

# views.py
from rest_framework import viewsets
from .models import Project
from .serializers import ProjectPolymorphicSerializer


class ProjectViewSet(viewsets.ModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectPolymorphicSerializer

Test it:

$ http GET "http://localhost:8000/projects/"
HTTP/1.0 200 OK
Content-Length: 227
Content-Type: application/json

[
    {
        "resourcetype": "Project",
        "topic": "John's gathering"
    },
    {
        "artist": "T. Turner",
        "resourcetype": "ArtProject",
        "topic": "Sculpting with Tim"
    },
    {
        "resourcetype": "ResearchProject",
        "supervisor": "Dr. Winter",
        "topic": "Swallow Aerodynamics"
    }
]
$ http GET "http://localhost:8000/projects/"
HTTP/1.0 200 OK
Content-Length: 227
Content-Type: application/json

[
    {
        "resourcetype": "Project",
        "topic": "John's gathering"
    },
    {
        "artist": "T. Turner",
        "resourcetype": "ArtProject",
        "topic": "Sculpting with Tim"
    },
    {
        "resourcetype": "ResearchProject",
        "supervisor": "Dr. Winter",
        "topic": "Swallow Aerodynamics"
    }
]
$ http POST "http://localhost:8000/projects/" resourcetype="ArtProject" topic="Guernica" artist="Picasso"
HTTP/1.0 201 Created
Content-Length: 67
Content-Type: application/json

{
    "artist": "Picasso",
    "resourcetype": "ArtProject",
    "topic": "Guernica"
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-rest-polymorphic-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_rest_polymorphic-0.1.1-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-rest-polymorphic-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-rest-polymorphic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6406826ef9bf469dea630df0768cb3a41e7b075708cda0e9af0b81a3c11d09cc
MD5 b3043b0d06e23a57e6801b00b919a1c1
BLAKE2b-256 3f02c4dfb220cf093c2344492ccc98398406244ab0b8fac5feb6fc40951ca84a

See more details on using hashes here.

Provenance

File details

Details for the file django_rest_polymorphic-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_polymorphic-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 aeccafa475d2aa9872504acb5e3d190bd30cbcb98c4fb2acce45df18aec7773a
MD5 dbda73df3d519e059fed05d5fce4755c
BLAKE2b-256 a8328f65188906eab8a7f8ef12a676514e0e54f0ba7c5bb7eccc44bd42f47cac

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page