将 django 数据序列化为 json 以与 jqgrid 一起使用的推荐方法是什么?

发布于 2024-10-03 20:49:55 字数 578 浏览 1 评论 0 原文

我整个晚上都在努力争取 jsonReader: 在 jqgrid 中正确设置以匹配 django 序列化器功能的 JSON 输出。从那以后,我厌倦了尝试,并决定我应该创建一个视图来提供输出 jqgrid 希望默认情况下使用 json 。我的计划是编写一个视图,形成一个看起来像 jqgrid 想要的字符串。这对我来说似乎有点难看(即大量手动格式化 JSON),但我认为如果我选择使用 jqgrid 的更多功能,这可能是未来最具扩展性的路线。从 Django 获取自定义格式 JSON 的推荐方法是什么?推荐的 Django 方法是什么来完成我将通过创建如下例所示的视图来完成的任务?

def serializedData(request):
    querySet = Model.objects.filter(date=None).order_by('id')
    data = '{'
    for row in querySet:
        # go through each item and make a pretty json row and add it to data
    data += '}'
    return HttpResponse(data)

I've been spending my evening fighting with getting jsonReader: set up correctly in jqgrid to match the JSON output of django's serializer capabilities. I have since gotten sick of trying and decided I should just make a view to give the output jqgrid wants json to be in by default. My plan is to write a view that forms a string that looks like jqgrid wants it. This seems a bit ugly to me (ie lots of manual formatting of JSON), but I think this is probably the most expandable route for the future should I choose to use more functionality of jqgrid. What is the recommended way of getting custom formatted JSON out of Django? What is the recommended Django way of accomplishing what I will accomplish by creating a view like the below example?

def serializedData(request):
    querySet = Model.objects.filter(date=None).order_by('id')
    data = '{'
    for row in querySet:
        # go through each item and make a pretty json row and add it to data
    data += '}'
    return HttpResponse(data)

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

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

发布评论

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

评论(3

瀞厅☆埖开 2024-10-10 20:49:55

这会做:

from django.utils import simplejson
return HttpResponse(simplejson.dumps(data), mimetype="application/json")

This will do:

from django.utils import simplejson
return HttpResponse(simplejson.dumps(data), mimetype="application/json")
若水般的淡然安静女子 2024-10-10 20:49:55

您可能应该查看一些可用的序列化/API 框架:

  • django-piston ,我不太喜欢它,因为我认为序列化不太灵活。
  • SpitEat,它是为允许非常灵活的序列化而构建的,但是该文档完全过时了......我不知道没有时间修复它...但是您可能可以通过阅读测试和源代码(完整记录并且文档是最新的)找到
  • 更多包 那里

You should probably check out some of the serialization/API frameworks available out there :

  • django-piston, which I don't like so much since the serialization is not so flexible in my opinion.
  • SpitEat, which is built for allowing very flexible serialization, however the doc is completely outdated ... I don't find the time to fix it... but you can probably find your way by reading the tests, and the source code (which is completely documented and doc is up-to-date)
  • more packages there
宛菡 2024-10-10 20:49:55

我最终创建了一个与我想要的 json 格式匹配的 django 模板。结果只有几行,所以我无法想象让它变得更简单,特别是当无论如何都必须手动分配名称时。

例如..类似于这样的东西:

[{% for herp in derps %}
    { "id":"{{ herp.id }}___", "value":"{{ herp.value }}"}{% if not forloop.last %},{% endif %}
{% endfor %}]

I ended up creating a django template that matched the json format I wanted. This turned out to be a couple of lines so I can't imagine making it any simpler, especially when the names have to be manually assigned anyway.

example..something similar to this:

[{% for herp in derps %}
    { "id":"{{ herp.id }}___", "value":"{{ herp.value }}"}{% if not forloop.last %},{% endif %}
{% endfor %}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文