This package provides ability for developers to create serializers directly from models.
- Python 3.7+
- Django 3.0+
- DRF 3+
pip install drf-extended
from django.db import models
from drf_extended import APIMixin, APIModel, APIField
from rest_framework import serializers
class MyParentModel(APIMixin, models.Model):
name = models.CharField(max_length=64)
description = models.TextField()
api_fields = [
APIField('name'),
APIField('description'),
]
class MyModel(APIMixin, models.Model):
title = models.CharField(max_length=40)
parent = models.ForeignKey(MyParentModel, on_delete=models.CASCADE)
api_fields = [
# Plain serialize title as rest_framework does it out of the box.
APIField('title'),
# Serializer from foreign key field's model will be calculated automatically,
# if ForeingKey related model is subclass of APIMixin.
APIField('parent'),
# You can specify custom serializer for field.
APIField('parent_id', serializers.IntegerField(source='parent.id')),
]
from .models import MyModel
MyModelSerializer = MyModel.get_serializer_class()
from rest_framework import serializers
from .models import MyParentModel
class MyParentModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyParentModel
fields = ('name', 'description')
class MyModelSerializer(serializers.ModelSerializer):
parent = MyParentModelSerializer()
class Meta:
fields = ('title', 'parent', 'parent_id')
from rest_framework.generics import RetrieveAPIView
from .models import MyModel
class MyModelRetrieveView(RetrieveAPIView):
serializer_class = MyModel.get_serializer_class()
queryset = MyModel.objects.select_related('parent').all()
Using such serializer in views allows us to get only those fields that we specify in the request query params with key fields
.
E.g. GET /some-model/<pk>/?fields=id,parent_id
, results us:
{
"id": <pk>,
"parent_id": <pk>
}