MyPostList.py
class PostList(viewsets.ModelViewSet):
queryset = Postinfo.objects.all().order_by('-postuid')[:10]
serializer_class = PostListSerializer
PostListSerializer.py
class PostListSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Postinfo
fields = ("content",
"useruid")
models.py
class Postinfo(models.Model):
postuid = models.BigAutoField(db_column='WorryUID', primary_key=True)
useruid = models.BigIntegerField(db_column='UserUID')
content = models.TextField(db_column='Content')
registerdate = models.DateTimeField(
db_column='RegisterDate', default=datetime.datetime.today().strftime("%Y-%m-%dT%H:%M:%S"))
class Meta:
managed = False
db_table = 'postinfo'
我想要响应pagenumber 如果我请求PostList我想要响应postlist和PageNo 像这样
queryset = Postinfo.objects.all().order_by('-postuid')[:10]
{"PostList":queryset,
"PageNo":1}
我发送pageNo = 2
queryset = Postinfo.objects.all().order_by('-postuid')[10:20]
{"PostList":queryset,
"PageNo":2}
我有两个问题
第一次请求(GET)成功PostList但无法添加PageNo
如果我发送PageNo它不是请求(GET)这是POST,所以调用create() 我想要返回PostList和PageNo,不希望mysql插入
答案 0 :(得分:1)
Django Rest Framework内置了分页系统。您不必手动创建分页。您只需pagination_class
查看您的观点即可添加分页。请阅读文档以获取更多详细信息。