Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BillBrower opened this issue Jan 15, 2018 · 2 comments
Closed

Comments

@BillBrower
Copy link

BillBrower commented Jan 15, 2018

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?

@mblayman
Copy link
Collaborator

Is this fixed by #402 being merged?

@BillBrower
Copy link
Author

It does. Thanks for fixing this so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants