Home > Software engineering >  Django rest framework doesn't display all fields and doesn't apply pagination
Django rest framework doesn't display all fields and doesn't apply pagination

Time:09-18

I don't have any idea what is causing this problem. Django rest framework doesn't display all fields when I want to post data, it also doesn't apply pagination. Here is code:

views.py

class BookApiList(APIView):
    
    def get(self, request, format = None):
        books = Book.objects.all()

        serializer = BookSerializer(books, many = True)
        return Response(serializer.data)
    
    def post(self, request, format = None):

        serializer = BookSerializer(data = request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status = status.HTTP_201_CREATED)
        return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)


class BookApiDetail(APIView):
    def get_object(self, name):
        try:
            book = Book.objects.get(title = name)
            return book
        except:
            raise Http404

    def get(self, request, name, format = None):
        book = self.get_object(name)
        serializer = BookSerializer(book)
        return Response(serializer.data)
    
    def put(self, request, name, format = None):
        book = self.get_object(name)
        serializer = BookSerializer(book, data = request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)

    def delete(self, request, name, format=None):
        book = self.get_object(name)
        book.delete()
        return Response(status = status.HTTP_204_NO_CONTENT)

models.py

class Book(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE, null = True, blank = True)
    image = models.ImageField(default = "Nana.jpg", upload_to = 'images/', null = True, blank = True)
    title = models.CharField(max_length = 150, unique = True)
    author = models.CharField(max_length = 100)
    category = models.CharField(max_length = 100)
    description = models.TextField(max_length = 5000, null = True, blank = True)
    published_date = models.DateField(null = True, blank = True)
    language = models.CharField(max_length = 100, null = True, blank = True, default = "Not selected")
    read_book_count = models.IntegerField(default = 0)


    def __str__(self):
        return self.title

serializers.py

class BookSerializer(serializers.ModelSerializer):

    class Meta:
        model = Book
        fields = [ 'title', 'author', 'category', 'description', 'published_date', 'language']

urls.py

path('library/api/books/', views.BookApiList.as_view(), name = 'api_list'),
path('library/api/books/<slug:name>/', views.BookApiDetail.as_view(), name = 'book_api_detail'),

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 1
}

When I go to first url link I get all data and this form: enter image description here

But it should give me a form with all my fields. Thanks!

CodePudding user response:

In user defined views(ex: APIView), you have to paginate queryset and return paginated response. and restframework cannot find which model and fields you are using in APIView, so instead of APIView use ListCreateAPIView

change BookApiList view like this:

from rest_framework import generics

class BookApiList(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  • Related