Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exception: Cannot get fields from a polymorphic serializer given a queryset #401

Closed
@BillBrower

Description

@BillBrower

I'm sure I'm just doing something wrong but the documentation on working with polymorphic resources is pretty sparse so I'm not sure what to change. I have a polymorphic model Parent with two instance types and when I try to retrieve them using DRF's ModelViewSet I get this error: Exception: Cannot get fields from a polymorphic serializer given a queryset.

Here are my views:

from rest_framework import permissions, viewsets
from rest_framework.authentication import TokenAuthentication
from .serializers import ParentSerializer

class ParentViewSet(viewsets.ModelViewSet):
    authentication_classes = (TokenAuthentication,)
    included = []
    pagination_class = None
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = ParentSerializer

And here is my serializer:

class ChildOneSerializer(serializers.ModelSerializer):
    class Meta:
        model = ChildOne
    .
    .
    .

class ChildTwoSerializer(serializers.ModelSerializer):
    class Meta:
        model = ChildTwo
    .
    .
    .

class ParentSerializer(PolymorphicModelSerializer):
    polymorphic_serializers = [ChildOneSerializer, ChildTwoSerializer]

    class Meta:
        model = Parent

I have my resource_names defined on my models. I've tried passing a list instead of a queryset and that works for an empty response ([]) but I run into issue #400 for non-empty responses. What should I be passing to my serializer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions