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

8

21

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?

Kendry answered 18/7, 2017 at 3:44 Comment(0)
G
19

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

Gauge answered 18/7, 2017 at 3:50 Comment(7)
lol and I confirmed what you just said in the docs. OK your right my bad thank youKendry
this brought a new issues which is 'ListSerializer' object is not callable currently researching itKendry
Interesting. Are you sure it's related? Because you're not using ListSerializer anywhere in your code example.Gauge
my CityListViewSet is inheriting from viewsets.ReadOnlyModelViewSet I wonder if that might be the causeKendry
Hmm. I just took a look at the DRF source and I don't see any reference to ListSerializer.Gauge
it is werid. Im going to see what I can findKendry
For a noob like me, it means in your CityListViewSet, add serializer-class = CitySerializerVarney
T
4

Add this code snippet to your views.py file

class CityListViewSet(viewsets.ReadOnlyModelViewSet):  # (viewsets.ModelViewSet) 
    serializer_class = CitySerializer
            
    queryset = City.objects.values('city').distinct()
    serializer = CitySerializer(queryset, many=True)
    ordering_fields = ('city',)
    ordering = ('city',)
Turnaround answered 16/3, 2022 at 23:35 Comment(0)
A
2
serializer = CitySerializer(queryset, many=True) 

The above line should be replaced with

serializer_class = CitySerializer(queryset, many=True)
Archdeaconry answered 12/12, 2020 at 6:47 Comment(0)
Y
2

error says you define a serializer attribute, you need to correct with writing serializer_class attribute in your code,

serializer_class = yourCreatedSerializer
Yah answered 2/7, 2021 at 5:49 Comment(0)
M
1

Here you used a different model name:

view:

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

import -> from .serializers import TaskSerializers,CitySerializer

serializer:

class CitySerializer(serializers.ModelSerializer):    
class Meta:
    model = City
    fields =('city',)
Mayfair answered 11/8, 2018 at 6:47 Comment(0)
M
1

i got this error when declared post method in view and trying to send post data without serialize, if you are doing the request from javascript i solved it using JSON.stringify()

Mealymouthed answered 15/2, 2020 at 19:22 Comment(0)
A
0

you have to override the user just add

from django.contrib.auth.models import User
from rest_framework.permissions import IsAdminUser

and in createViewList

permission_classes = [IsAdminUser]
Alfalfa answered 16/2, 2021 at 8:3 Comment(0)
T
0

Rename this to

    serializer = CitySerializer(queryset, many=True)

This

    serializer_class = yourCreatedSerializer

Your job is done

Tavey answered 10/2, 2023 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.