Django Viewset分页会抛出序列化错误

时间:2022-02-28 15:45:38

I have something like this:

我有这样的东西:

Paginating Viewset:

方法Viewset:

class FeedViewSet(ModelViewSet):

    queryset = Feed.objects.all()
    serializer_class = FeedSerializer

    def list(self, request, *args, **kwargs):

        paginator = pagination.LimitOffsetPagination()
        paginator.default_limit = 15
        paginator.limit = paginator.get_limit(request)
        paginator.offset = paginator.get_offset(request)
        feeds = paginator.paginate_queryset(Feed.objects.all(), request)
        return Response(
                data={
                        'feeds': feeds,
                        'limit': paginator.limit,
                        'offset': paginator.offset,
                        'overall_count': paginator.count
                    }
                )

Feeds Model:

源模型:

class Feed(Base):
    headline = models.CharField(max_length=255)
    link = models.CharField(max_length=255, unique=True)
    summary = models.TextField()
    published_date = models.DateTimeField()
    views = models.IntegerField(default=0)
    shares = models.IntegerField(default=0)
    source = models.ForeignKey(Source, on_delete=models.CASCADE, )
    reader = models.ManyToManyField(User, through='Bookmark')

Feed Serializer:

饲料序列化器:

class FeedSerializer(serializers.ModelSerializer):
    class Meta:
        fields = (
            'id',
            'headline',
            'link',
            'summary',
            'published_date',
            'views',
            'shares',
            'source',
            'created',
            'modified',

        )
        model = models.Feed

I am new to Django. I might be absolutely wrong here.

我是Django的新手。我可能完全错了。

I have gone through official DRF here : http://www.django-rest-framework.org but couldn't figure out the correct way of implementing pagination.

我在这里查看了官方的DRF: http://www.django-rest-framework.org,但是无法找到实现分页的正确方法。

I get something like this : TypeError: Object of type 'Feed' is not JSON serializable

我得到了这样的结果:TypeError:“提要”类型的对象不是JSON可序列化的

What am I doing wrong? If this is not the right way to do it, how should it be done. What changes do I need to make?

我做错了什么?如果这不是正确的方法,应该怎么做。我需要做什么改变?

1 个解决方案

#1


1  

You shoud pass to Response serialized feeds data. For this you need to use serializer:

您应该传递到响应序列化的提要数据。为此,您需要使用序列化器:

def list(self, request, *args, **kwargs):
    serializer = self.get_serializer_class()
    paginator = pagination.LimitOffsetPagination()
    paginator.default_limit = 15
    paginator.limit = paginator.get_limit(request)
    paginator.offset = paginator.get_offset(request)
    feeds = paginator.paginate_queryset(Feed.objects.all(), request)
    return Response(
            data={
                    'feeds': serializer(feeds, many=True).data,
                    'limit': paginator.limit,
                    'offset': paginator.offset,
                    'overall_count': paginator.count
                }
            )

#1


1  

You shoud pass to Response serialized feeds data. For this you need to use serializer:

您应该传递到响应序列化的提要数据。为此,您需要使用序列化器:

def list(self, request, *args, **kwargs):
    serializer = self.get_serializer_class()
    paginator = pagination.LimitOffsetPagination()
    paginator.default_limit = 15
    paginator.limit = paginator.get_limit(request)
    paginator.offset = paginator.get_offset(request)
    feeds = paginator.paginate_queryset(Feed.objects.all(), request)
    return Response(
            data={
                    'feeds': serializer(feeds, many=True).data,
                    'limit': paginator.limit,
                    'offset': paginator.offset,
                    'overall_count': paginator.count
                }
            )