Polymorphic serializers for Django REST Framework.
Project description
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 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
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
Built Distribution
File details
Details for the file django-rest-polymorphic-0.1.3.tar.gz
.
File metadata
- Download URL: django-rest-polymorphic-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68e64f0f64fcea9a50dc57268462c5680e15641e4c70a0358cf5b74cb6da59c1 |
|
MD5 | b86897b653683cd4ddf4e2cd92cd4db6 |
|
BLAKE2b-256 | ea1f0b08fa154f2bfcc9be5564591eecbd5508646a51c5e22e82ee3151b9a84c |
Provenance
File details
Details for the file django_rest_polymorphic-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: django_rest_polymorphic-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fea20d8b9dd7c6cdd352e70d6cac602cbb33d986436577500c62ed4c7362b6d |
|
MD5 | 27f227d40c4dd0e144172502520f4b8e |
|
BLAKE2b-256 | c8ca78a8d9ddc205f35e930b6ba7d50a5e3ed5d631858080d975e85a0d8c4d17 |