20

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?

Aneesh R S
  • 3,807
  • 4
  • 23
  • 35

8 Answers8

18

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

Cory Madden
  • 5,026
  • 24
  • 37
  • 1
    lol and I confirmed what you just said in the docs. OK your right my bad thank you –  Jul 18 '17 at 03:52
  • this brought a new issues which is `'ListSerializer' object is not callable` currently researching it –  Jul 18 '17 at 03:54
  • Interesting. Are you sure it's related? Because you're not using ListSerializer anywhere in your code example. – Cory Madden Jul 18 '17 at 03:55
  • my `CityListViewSet` is inheriting from `viewsets.ReadOnlyModelViewSet` I wonder if that might be the cause –  Jul 18 '17 at 03:56
  • Hmm. I just took a look at the DRF source and I don't see any reference to ListSerializer. – Cory Madden Jul 18 '17 at 03:59
  • it is werid. Im going to see what I can find –  Jul 18 '17 at 04:00
  • 1
    For a noob like me, it means in your `CityListViewSet`, add `serializer-class = CitySerializer` – Darpan Nov 05 '19 at 17:33
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',)
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Aje
  • 41
  • 2
2
serializer = CitySerializer(queryset, many=True) 

The above line should be replaced with

serializer_class = CitySerializer(queryset, many=True)
Saeed
  • 3,294
  • 5
  • 35
  • 52
maguluri
  • 75
  • 1
  • 1
2

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

serializer_class = yourCreatedSerializer
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
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',)
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()

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]
Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
0

Rename this to

    serializer = CitySerializer(queryset, many=True)

This

    serializer_class = yourCreatedSerializer

Your job is done