我整个晚上都在努力争取 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)
发布评论
评论(3)
这会做:
This will do:
您可能应该查看一些可用的序列化/API 框架:
You should probably check out some of the serialization/API frameworks available out there :
我最终创建了一个与我想要的 json 格式匹配的 django 模板。结果只有几行,所以我无法想象让它变得更简单,特别是当无论如何都必须手动分配名称时。
例如..类似于这样的东西:
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: