在一个 HttpResponse 中返回两件事

发布于 2024-09-25 04:44:39 字数 926 浏览 3 评论 0原文

我正在尝试跨ajax调用实现分页。当用户想要查看下一个 x 数量的结果时,页面不应刷新。

这是我的问题。返回 QuerySet 非常简单。我只是这样做(sumaJson 是自定义的)

data = serializers.serialize('sumaJson', result_page.object_list, relations=('first_major', 'country_of_origin', 'second_major'))
return HttpResponse(data, mimetype="application/json") 

现在我也想返回诸如此类的东西

result_page.has_previous()
result_page.has_next()
result_page.paginator.count

。我一辈子都不知道如何在一个回复中同时表达这两点。我无法将此信息添加到 result_page.object_list 因为序列化程序会失败。如果我是这样的话,

simplejson.dumps(paging_info + result_page.object_list)

那么在 javascript 中,QuerySet 不再是对象列表,而只是一大串无法解释的字符,

$.each(data.data, function(index, item){

我尝试了一些糟糕的技巧,例如创建一个假对象并将其放入 object_list ,序列化它,然后删除该对象。这使我能够获取数据。但是,我不想创建和删除假对象。

我不想干扰序列化器。一旦我获取查询集以获取分页信息,我就不想发送第二个ajax请求。

我错过了什么吗?有没有一种简单的方法可以在一个回复中同时传达这两个内容?谢谢!

I am trying to implement paging across ajax calls. The page should not refresh when the user wants to see the next x num of results.

Here is my problem. Returning the QuerySet is super simple. I just do (sumaJson is custom)

data = serializers.serialize('sumaJson', result_page.object_list, relations=('first_major', 'country_of_origin', 'second_major'))
return HttpResponse(data, mimetype="application/json") 

Now I also want to return things like

result_page.has_previous()
result_page.has_next()
result_page.paginator.count

and so on. I for the life of me can't figure out how to get both across in one response. I can't add this info to result_page.object_list because then the serializer fails. If I something of the sort of

simplejson.dumps(paging_info + result_page.object_list)

Then in the javascript the QuerySet is no longer a list of objects but just a big string of characters which can't be interpreted with

$.each(data.data, function(index, item){

I tried some bad hacks like creating a fake object and putting it in the object_list, serializing this and then deleting the object. This allows me to get the data across. However, I don't want to be creating and deleting fake objects.

I don't want to meddle with the serializer. I don't want to send a second ajax request once I get the querySet back to get the paging info.

Am I missing something? Is there an easy way to get both across in one response? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾冷 2024-10-02 04:44:39

当我序列化对象集合时,我通常将分页信息包含在响应正文本身中。如果我有 50 个对象,我想每页提供 10 个对象,那么 JSON 将如下所示:

{
    "count": 50,
    "objects": [
        {
            ...
        }
    ],
    "pages": {
        "count": 5,
        "current": "http://api.example.com/objects/?page=3",
        "first": "http://api.example.com/objects/",
        "last": "http://api.example.com/objects/?page=5",
        "next": "http://api.example.com/objects/?page=4",
        "previous": "http://api.example.com/objects/?page=2"
    }
}

When I'm serializing a collection of objects, I typically include the pagination information in the response body itself. If I had 50 objects that I wanted to serve up 10 per page, the JSON would look something like this:

{
    "count": 50,
    "objects": [
        {
            ...
        }
    ],
    "pages": {
        "count": 5,
        "current": "http://api.example.com/objects/?page=3",
        "first": "http://api.example.com/objects/",
        "last": "http://api.example.com/objects/?page=5",
        "next": "http://api.example.com/objects/?page=4",
        "previous": "http://api.example.com/objects/?page=2"
    }
}
七颜 2024-10-02 04:44:39

我过去对 ajax 调用所做的是将 json 作为 HttpResponse 返回,就像您正在做的那样,并为我想要返回的任何其他字段添加标头。

data = serializers.serialize('sumaJson', result_page.object_list, relations=('first_major', 'country_of_origin', 'second_major'))
response = HttpResponse(data, mimetype="application/json") 
response['X-VALUE'] = 'asdf' #this is the header, you can create as many of these as you'd like
return response

在 javascript 方面...

$.ajax({
  url: '/whatever/here/',
  success: function(data, code, xhr) {
    alert(xhr.getResponseHeader('X-VALUE'));
  }
});

希望这会有所帮助。

What I've done in the past for ajax calls is I return the json as the HttpResponse, like you're doing, and add a header for any additional fields that I want to return.

data = serializers.serialize('sumaJson', result_page.object_list, relations=('first_major', 'country_of_origin', 'second_major'))
response = HttpResponse(data, mimetype="application/json") 
response['X-VALUE'] = 'asdf' #this is the header, you can create as many of these as you'd like
return response

and on the javascript side...

$.ajax({
  url: '/whatever/here/',
  success: function(data, code, xhr) {
    alert(xhr.getResponseHeader('X-VALUE'));
  }
});

hope this helps.

乱世争霸 2024-10-02 04:44:39

simplejson.dumps() 可以深度序列化字典,甚至可以递归地序列化。

您可以采取两种方法来实现此目的。第一个是在 QuerySets 上使用 Django ORM 的 value() 方法:它使用对象 ID 而不是引用生成纯 Python 字典,适合序列化。

如果您需要比这更深入的内容,您可能需要编写一些内容来创建 jpwatts 提出的字典结构。如果您需要这种功能,我我的个人博客上有一篇关于向 simplejson 添加函子、生成器、迭代器和闭包的文章。该示例演示了如何将 Treebeard 树结构转换为 javascript 对象。

代码是:

from django.utils.simplejson.encoder import JSONEncoder

class ExtJsonEncoder(JSONEncoder):
    def default(self, c):
        # Handles generators and iterators
        if hasattr(c, '__iter__'):
            return [i for i in c]

        # Handles closures and functors
        if hasattr(c, '__call__'):
            return c()

        return JSONEncoder.default(self, c)

jpwatts 有正确的方法。您可能只需要编写一些代码即可到达那里。

simplejson.dumps() can serialize dictionaries deeply, even recursively.

There are two approaches you can take to this. The first is to use the Django ORM's values() method on QuerySets: it churns out pure python dictionaries with object IDs instead of references, suitable for serializing.

If you need even deeper than that, you may have to write something to create the dictionary structure proposed by jpwatts. If you need that kind of power, I have a post on my personal blog about adding functors, generators, iterators, and closures to simplejson. The example demonstrates how to turn a Treebeard tree structure into a javascript object.

The code is:

from django.utils.simplejson.encoder import JSONEncoder

class ExtJsonEncoder(JSONEncoder):
    def default(self, c):
        # Handles generators and iterators
        if hasattr(c, '__iter__'):
            return [i for i in c]

        # Handles closures and functors
        if hasattr(c, '__call__'):
            return c()

        return JSONEncoder.default(self, c)

jpwatts has the correct approach. You may just have to write some code to get there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文