1. Introduction

If you have a requirement that your Django project API needs to return serialized data from two model classes at the same time, you might want to implement the query one by one and return it as a Response object instead. I won’t!

The Django-rest-multiple-models component will allow you to serialize multiple models with only your configuration information.

2. PIP installation

Install with python-pip:

python3 -m pip install django-rest-multiple-models
Copy the code

3. Configure and use basic information

Register component APP in Django project settings.py:

INSTALLED_APPS = [
    ...
    'drf_multiple_model',]Copy the code

At this point you can import the module and implement your functionality, such as:

# 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()
Copy the code
# Serializers
class PlaySerializer(serializers.ModelSerializer) :
    class Meta:
        model = Play
        fields = ('genre'.'title'.'pages')


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

In the above example we defined Models and Serializer. Now we can write the API view class directly:

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},
    ]
Copy the code

You can get response data like the following:

{
    '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},... ] }Copy the code

You can also inherit using FlatMultipleModelAPIView:

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},
        ...
    ]
Copy the code

You will get:

[
    {'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'},
    ...
]
Copy the code

Of course you can filter and paginate, as well as other attributes, such as using tags, etc. Here is a document for you: official documentation

Success is the ability to go from one failure to another with no loss of enthusiasm. — Winston Churchill

Success is a man’s ability to go from one failure to another without loss of enthusiasm. — Winston Churchill