Skip to content

This package provides ability for developers to create serializers directly from models.

License

Notifications You must be signed in to change notification settings

floydya/drf-extended

lint deploy pypi

DRF extended

This package provides ability for developers to create serializers directly from models.

Installation

Supported:

  • Python 3.7+
  • Django 3.0+
  • DRF 3+

Install using pip:

pip install drf-extended

Usage

Models file:

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')),
    ]

Get serializer:

from .models import MyModel

MyModelSerializer = MyModel.get_serializer_class()

Here, MyModelSerializer is equal to:

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')

Use in views:

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>
}

About

This package provides ability for developers to create serializers directly from models.

Resources

License

Code of conduct

Stars

Watchers

Forks

Languages