django modelviewset响应添加其他信息,当我请求发布时不想调用create()

时间:2018-05-09 07:57:35

标签: django django-models django-rest-framework django-serializer

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}

我有两个问题

  1. 第一次请求(GET)成功PostList但无法添加PageNo

  2. 如果我发送PageNo它不是请求(GET)这是POST,所以调用create() 我想要返回PostList和PageNo,不希望mysql插入

1 个答案:

答案 0 :(得分:1)

Django Rest Framework内置了分页系统。您不必手动创建分页。您只需pagination_class查看您的观点即可添加分页。请阅读文档以获取更多详细信息。

http://www.django-rest-framework.org/api-guide/pagination/