Skip to content
프로젝트

MattBroach/DjangoRestMultipleModels

GitHub에서 보기

소개

Django REST Framework에서 여러 모델이나 쿼리셋을 직렬화하는 View(믹스인으로도 사용 가능)

웹 및 앱 개발

이 수치를 측정하는 방법

What you can do

  • Send multiple serialized models as separate arrays, one merged list, or a single JSON object
  • Sort different serialized models using shared fields
  • Filtering -- either per queryset or on all querysets
  • custom model labeling

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

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 ObjectMultipleModelAPIView

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, one merged list, or a single JSON object
  • Sort different serialized models using shared fields
  • pagination
  • Filtering -- either per queryset or on all querysets
  • custom model labeling

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

Basic Usage

drf-multiple-model comes with two generic class-based-view for serializing multiple models: the ObjectMultipleModelAPIView and the FlatMultipleModelAPIView. Both views require a querylist attribute, which is a list or tuple of dicts containing (at minimum) a queryset key and a serializer_class key; the main difference between the views is the format of the response data. 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 ObjectMultipleModelAPIView as follows:

from drf_multiple_model.views import ObjectMultipleModelAPIView

class TextAPIView(ObjectMultipleModelAPIView):
    querylist = [
        {'queryset': Play.objects.all(), 'serializer_class': PlaySerializer},
        {'queryset': Poem.objects.filter(style='Sonnet'), 'serializer_class': 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},
        ....
    ],
}

Or you could use the FlatMultipleModelAPIView as follows:

from drf_multiple_model.views import FlatMultipleModelAPIView

class TextAPIView(FlatMultipleModelAPIView):
    querylist = [
        {'queryset': Play.objects.all(), 'serializer_class': PlaySerializer},
        {'queryset': Poem.objects.filter(style='Sonnet'), 'serializer_class': PoemSerializer},
        ....
    ]

which would return::

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

관련 프로젝트 살펴보기

불러오는 중…