'CityListViewSet' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method

user6781560 picture user6781560 · Jul 18, 2017 · Viewed 17.2k times · Source

I am assuming by the error in the title, once more here for clarity

'CityListViewSet' should either include a `serializer_class` attribute, 
or override the `get_serializer_class()` method.

that my serializer isn't connected to my view, which in my code it should be. I'm not really sure where the bug is in this one. I wonder if any of you have seen something similar?

Here is the code.

Router:

router.register(r'city-list', CityListViewSet, base_name='city-list')

view:

class CityListViewSet(viewsets.ReadOnlyModelViewSet):                 
    queryset = Venue.objects.values('city').distinct()
    serializer = CitySerializer(queryset, many=True)
    ordering_fields = ('city',)
    ordering = ('city',)

serializer:

class CitySerializer(serializers.ModelSerializer):    
    class Meta:
        model = City
        fields =('city',)

what is it that would be causing such an assertion error with the code seemly wired up correctly?

Answer

Cory Madden picture Cory Madden · Jul 18, 2017

The exception says it itself. You need a serializer_class attribute. You have serializer.