This issue has similar headings to other threads on this forum, but I'm sure this is not a duplicate because one because I didn't overwrite my pagination details or anything (which turned out to be the cause in other questions).
Anyways, Django raised to_representation()
must be implemented. I'm completely stuck as there is no relevant documentation regarding this (please comment if you have some) and my case didn't fit in with other people's.
For those of you that think I didn't include everything in the error message, the error message is just this short and ambiguous. View this on github issues
Code:
settings
INSTALLED_APPS = ['rest_framework',]
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
view
from rest_framework import viewsets
from .models import Todo
from .serializers import TodoSerializer
class TodoViewSet(viewsets.ModelViewSet):
queryset = Todo.objects.all()
serializer_class = TodoSerializer
serializers
from .models import Todo
from rest_framework import serializers
class TodoSerializer(serializers.BaseSerializer):
class Meta:
model = Todo
fields = ['title', 'desc', 'level', 'created']
urls
from django.urls import path, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'todos', views.TodoViewSet)
urlpatterns = [
path('api/', include(router.urls)),
]
Many thanks in advance. Note: in case you're wondering is this all, this IS all as I jus started to learn drf a few moments ago.
you must use class TodoSerializer(serializers.ModelSerializer):
not BaseSerializer