pythondjango-rest-frameworkassertiondjango-rest-viewsets

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


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?


Solution

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