ELIYAHUT123 / DjangoRestMultipleModels

View (and mixin) for serializing multiple models or querysets in Django Rest Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status PyPI version

Multiple Model View

Django Rest Framework provides some incredible tools for serializing data, but sometimes you need to combine many serializers and/or models into a single API call. drf-multiple-model is an app designed to do just that.

Installation

Install the package from pip:

pip install django-rest-multiple-models

Make sure to add 'drf_multiple_model' to your INSTALLED_APPS:

INSTALLED_APPS = (
    ....
    'drf_multiple_model',
)

Then simply import the view into any views.py in which you'd want to use it:

from drf_multiple_model.views import MultipleModelAPIView

Note: This package is built on top of Django Rest Framework's generic views and serializers, so it presupposes that Django Rest Framework is installed and added to your project as well.

Features

  • Send multiple serialized models as separate arrays or one merged list
  • Sort different serialized models using shared fields
  • pagination (sort of)
  • Filtering -- either per queryset or on all queryset
  • custom model labeling

For full configuration options, filtering tools, and more, see the documentation.

Basic Usage

drf-multiple-model comes with the MultipleModelAPIView generic class-based-view for serializing multiple models. MultipleModelAPIView requires a queryList attribute, which is a list or tutple of queryset/serializer pairs (in that order). For example, let's say you have the following models and serializers:

# Models
class Play(models.Model):
    genre = models.CharField(max_length=100)
    title = models.CharField(max_length=200)
    pages = models.IntegerField()

class Poem(models.Model):
    title = models.CharField(max_length=200)
    style = models.CharField(max_length=100)
    lines = models.IntegerField()
    stanzas = models.IntegerField()

# Serializers
class PlaySerializer(serializers.ModelSerializer):
    class Meta:
        model = Play
        fields = ('genre','title','pages')

class PoemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Poem
        fields = ('title','stanzas')

Then you might use the MultipleModelAPIView as follows:

from drf_multiple_model.views import MultipleModelAPIView

class TextAPIView(MultipleModelAPIView):
    queryList = [
        (Play.objects.all(),PlaySerializer),
        (Poem.objects.filter(style='Sonnet'),PoemSerializer),
        ....
    ]

which would return:

[
    {
        'play' : [
                {'genre': 'Comedy', 'title': "A Midsummer Night's Dream", 'pages': 350},
                {'genre': 'Tragedy', 'title': "Romeo and Juliet", 'pages': 300},
                ....
            ],
    },
    {
        'poem' : [
                {'title': 'Shall I compare thee to a summer's day?', 'stanzas': 1},
                {'title': 'As a decrepit father takes delight', 'stanzas': 1},
                ....
            ],
    }
]

About

View (and mixin) for serializing multiple models or querysets in Django Rest Framework

License:MIT License


Languages

Language:Python 100.0%