Polymorphically serializing objects in Django

Question:

I have the following object inheritance model:

class Room:
    name = models.CharField(db_index=True, unique=True, max_length=255)
    status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
    members = models.ManyToManyField(User)
    last_activity = models.DateTimeField(default=timezone.now)

And the inherited models are:

class LeagueRoom(Room):
    league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
    logo_url = models.CharField(max_length=1024, null=True)

and:

class ClubRoom(Room):
    club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
    logo_url = models.CharField(max_length=1024, null=True)

The respective Serializers are as follows:

class RoomSerializer(serializers.ModelSerializer):
    members = UserSerializer(read_only=True, many=True)

    class Meta:
        model = Room
        fields = ('id', 'name', 'status', 'members', 'created', 'modified', 'last_active')

and

class LeagueRoomSerializer(serializers.ModelSerializer):
    location = LocationSerializer(read_only=True)
    league = LeagueSerializer(read_only=True)
    room = RoomSerializer(read_only=True)

    class Meta:
        model = LeagueRoom
        fields = ('id', 'name', 'location', 'status', 'league',
                  'logo_url', 'room', 'created', 'modified',)

and:

class ClubRoomSerializer(serializers.ModelSerializer):
    location = LocationSerializer(read_only=True)
    club = ClubSerializer(read_only=True)
    room = RoomSerializer(read_only=True)

    class Meta:
        model = ClubRoom
        fields = ('id', 'name', 'location', 'club', 'logo_url',
                  'status', 'room', 'created', 'modified',)

My problem is that I have fetched all rooms for an user in the following manner.

rooms = user.room_set.order_by('-last_activity')

Now I want to Serialize this data based on the room type. Thus instead of using the RoomSerializer I want to traverse the list of rooms and if the room is ClubRoom, then use ClubRoomSerializer or else LeagueRoomSerializer. How can I determine the child object from the parent?

Asked By: Melissa Stewart

||

Answers:

class RoomSerializer(serializers.Serializer):
    def to_representation(self, instance):
        if isinstance(instance, LeagueRoom)
            serializer_class = LeagueRoomSerializer
        elif isinstance(instance, ClubRoom):
            serializer_class = ClubRoomSerializer
        return serializer_class(instance=instance).data

queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
Answered By: Code Review Doctor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.