Django Viewset 分页抛出序列化错误
Django Viewset Pagination throws serialization error
我有这样的东西:
分页视图集:
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
}
)
供稿模型:
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 序列化程序:
class FeedSerializer(serializers.ModelSerializer):
class Meta:
fields = (
'id',
'headline',
'link',
'summary',
'published_date',
'views',
'shares',
'source',
'created',
'modified',
)
model = models.Feed
我是 Django 的新手。我在这里可能完全错了。
我在这里查看了官方 DRF : http://www.django-rest-framework.org 但无法找出实现分页的正确方法。
我得到这样的结果:TypeError: Object of type 'Feed' is not JSON serializable
我做错了什么?如果这不是正确的做法,那么应该怎么做。
我需要做哪些改变?
您应该传递给 Response
序列化的提要数据。为此,您需要使用序列化程序:
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
}
)
我有这样的东西:
分页视图集:
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
}
)
供稿模型:
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 序列化程序:
class FeedSerializer(serializers.ModelSerializer):
class Meta:
fields = (
'id',
'headline',
'link',
'summary',
'published_date',
'views',
'shares',
'source',
'created',
'modified',
)
model = models.Feed
我是 Django 的新手。我在这里可能完全错了。
我在这里查看了官方 DRF : http://www.django-rest-framework.org 但无法找出实现分页的正确方法。
我得到这样的结果:TypeError: Object of type 'Feed' is not JSON serializable
我做错了什么?如果这不是正确的做法,那么应该怎么做。 我需要做哪些改变?
您应该传递给 Response
序列化的提要数据。为此,您需要使用序列化程序:
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
}
)