1
votes

I'm developing a todo API with Django. I am getting an error as ToDoView(view class name) should include serializer_class but I already have it. Here is the code

Router and URL

router = routers.DefaultRouter()                      
router.register(r'todo', views.ToDOView ,'todo')     

urlpatterns = [
    path('admin/', admin.site.urls),       
    path('api/', include(router.urls))               
]

View:

class ToDOView(viewsets.ModelViewSet):
    serializer_class: ToDOserializer
    queryset = ToDo.objects.all()

serializers:

class ToDOserializer(serializers.ModelSerializer):
    class Meta:
        model : ToDo
        fields : ('id','title','description','completed')

id, title, description and completed are fields of my model

Error: Get this error.

AssertionError at /api/todo/ 'ToDOView' should either include a serializer_class attribute, or override the get_serializer_class() method

1

1 Answers

1
votes

it should be =(equal to) instead of :

class ToDOView(viewsets.ModelViewSet):
    serializer_class =  ToDOserializer
    queryset = ToDo.objects.all()

also do the same in serializer.py

class ToDOserializer(serializers.ModelSerializer):
    class Meta:
        model = ToDo
        fields = ('id','title','description','completed')