Django Rest分页的Content-Range配置 [英] Content-Range configuration for Django Rest Pagination

查看:205
本文介绍了Django Rest分页的Content-Range配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

6.30.15 - 我如何使这个问题更好,更有助于其他人?反馈应该是有帮助的谢谢!

我需要将内容范围标题发送到dojo / dgrid请求:

I need to send a content-range header to a dojo/dgrid request:

我找不到任何这样做的例子。我不知道这个设置在哪里(Content-Range:items 0-9 / *)。我已经在这个问题上给出了一个很好的linkheaderpagination示例: Django Rest框架分页设置 - 内容范围但是我不知道如何使这项工作产生一个Content-Range响应。任何接受者或任何人都知道任何好的资源或例子?

I cannot find any examples of HOW to do this. I'm not exactly sure where this setting goes (Content-Range: items 0-9/*). I have been given a great linkheaderpagination example on this question: Django Rest Framework Pagination Settings - Content-Range But I don't know how to make this work to produce a Content-Range response. Any takers or does anyone know of any good resources or examples??

更新:我正在Dojo /网格中创建分页。我正在使用服务器端api(Django Rest Framework)向Dojo / Dgrid提供数据。当Django Rest Framework从Dojo获取响应时,Django Rest Framework不会自动发送内容范围头文件。格式化时,Dojo发送范围请求以进行分页。我现在不知道如何配置Django Rest Framework API,以便在获取Dojo的请求时发送内容范围标头。不幸的是,我正在努力做一些非常具体的事情,而且任何一方的一般设置都不起作用。

UPDATE: I am trying to create pagination in Dojo/grid. I have am using a server-side api (Django Rest Framework) to supply to data to the Dojo/Dgrid. Django Rest Framework does not automatically sent content-range headers when it gets a response from Dojo. Dojo sends a range request when formatted to have pagination. I don't know now to configure the Django Rest Framework API to send a content-range header when it gets a request from Dojo. Unfortunately, I'm trying to do something very specific and just general settings on either side doesn't work.

推荐答案

如果您正在谈论在响应中提供Content-Range,我在对另一个SO问题的答复(我相信也可能来自您的团队?),这个标题有一个替代方法:如果你的回复格式是一个对象(不只是一个项目数组),它可以指定一个总计属性,表示项目总数。

If you are talking about providing Content-Range in the response, I mentioned in an answer to another SO question (which I believe may also have originated from your team?) that there is one alternative to this header: if your response format is an object (not just an array of items), it can specify a total property indicating the total number of items instead.

再次在 DRF文件中查找几分钟,似乎应该可以自定义响应的格式。

Again looking for a few minutes at the DRF documentation, it appears it should be possible to customize the format of the response.

根据文档和阅读LimitOffsetPagination的来源(你想要使用dstore工作,如已经在上一个问题

Based on the docs and reading the source of LimitOffsetPagination (which you want to be using to work with dstore, as already discussed in a previous question), if I had to take a wild guess, you should be able to do the following server-side:

class CustomPagination(pagination.LimitOffsetPagination):
    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('total', self.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('items', data)
        ]))

这意图将计数分配给总计,数据为项目以符合 dstore / Request 的期望。 ( next 以前的对于dstore而言是完全不必要的,所以你可以采取或离开它们,这取决于你在别处有任何用处。)

This purposely assigns the count to total and the data to items to align with the expectations of dstore/Request. (next and previous are entirely unnecessary as far as dstore is concerned, so you could take or leave them, depending if you have any use for them elsewhere.)

这篇关于Django Rest分页的Content-Range配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆