1

I walked through the documentation at https://django-model-utils.readthedocs.io/en/latest/managers.html to learn more about the InheritanceManager.I won't add the models.py because it is the same as in the documentation. So, here is my views.py:

class PlaceList(generics.ListAPIView):

    serializer_class = PlaceSerializer

    def get_queryset(self):
        return Place.objects.select_subclasses()

As you can see, the line return Place.objects.select_subclasses() returns all the Bar, Restaurant or Place instance stored in the database.

My serializers.py looks like the following:

from rest_framework import serializers
from genericRelations.models import Place, Restaurant, Bar

class PlaceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('id', 'location')

class RestaurantSerializer(serializers.ModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('id', 'location', 'restaurant_name')


class BarSerializer(serializers.ModelSerializer):
    class Meta:
        model = Bar
        fields = ('id', 'location', 'bar_name')

I do not know how to modify the PlaceSerializer so that it also serializes/deserializes the subclasses of Place. E.g. something like 'if a Bar instance is encountered, then use the BarSerializer' and 'if a Restaurant instance is encountered, then use the RestaurantSerializer'.

Note: When the URL is .../places/ then PlaceList is performed which results in this output:

{
    "places": [
        {
            "id": 1, 
            "location": "Köln"
        }, 
        {
            "id": 2, 
            "location": "Köln"
        }, 
        {
            "id": 3, 
            "location": "Köln"
        }, 
        {
            "id": 4, 
            "location": "Köln"
        }, 
        {
            "id": 5, 
            "location": "Köln"
        }
    ]
}

But what I want is something like the following (a mix between Restaurant and Bars):

{
    "places": [
        {
            "id": 1, 
            "location": "Köln"
            "bar_name": "BarName 1"
        }, 
        {
            "id": 2, 
            "location": "Köln"
            "restaurant_name": "RestaurantName 1"
        }, 
        {
            "id": 3, 
            "location": "Köln"
            "bar_name": "BarName 2"
        }, 
        {
            "id": 4, 
            "location": "Köln"
            "bar_name": "BarName 3"
        }, 
        {
            "id": 5, 
            "location": "Köln"
            "restaurant_name": "RestaurantName 2"

        }
    ]
}

How do I have to change my serializers.py file so that Django can use the right serializer for the right model ?

abdullah celik
  • 327
  • 2
  • 12

1 Answers1

0

I don't know a good way to do this, but there is a package https://pypi.org/project/django-rest-polymorphic/ that provides polymorphic serializers for Django Rest Framework. DRF doesn't, AFAIK, provide for polymorphism in ModelSerializers.

You could use a Serializer and implement all the methods to detect and return the correct fields based on the data, but that's non-trivial.

Joshua Smith
  • 6,561
  • 1
  • 30
  • 28