问题描述
我目前正在开发Django项目,并尝试实现WebDataRocksWebDataRocks是用于数据分析和可视化的免费Web报告工具
I currently develop a Django project and try to implement WebDataRocksWebDataRocks is a free web reporting tool for data analysis and visualization
我可以工作,但是我的问题是如何正确显示要在Webdatarocks中更新的数据我想更新我的每个模型.
I works but my problem deal with correctly presenting data to be updated in WebdatarocksI would like to update each of my models.
我在加载WebDataRocks的模板中使用了视图名称数据
I have a views name data use with my template that load WebDataRocks
def data(request):
data = serializers.serialize("json", mymodel.objects.filter(med_ide__lte=10))
return render(request, 'myapp/data.html', {'data':data})
我不太了解json的生成方式,因为我得到了以下格式:
I do not really understand the way json is produce because I get this format:
[
{
"model": "myapp.mymodel",
"pk": 1,
"fields":
{
"var1": 1,
"var2": "ABC",
"var3": "code",
"var4": "text",
"var5": null,
"var6": "'text'",
"var7": null
}
},
{
"model": "myapp.mymodel",
....
}
]
我在webdatarocks表中只能访问的2个变量是myapp.mymodel和pk我尝试使用data ['fields']之类的东西仅提取我需要的部分数据(=字段),但这不是正确的语法
The only 2 variables I get access in webdatarocks table are myapp.mymodel and pkI try to extract only part of my data I need (=fields) using things like data['fields'] but it is not the right syntax
怎么了?
推荐答案
我找到了一个解决方案(如果这是一个很好的解决方案,但似乎可行,则不要),它使用传递给上下文的列表而不是使用序列化器数据= json.dumps(列表(Medicament.objects.filter(med_ide__lte = 10).values('med_ide','med_num','med_dru')))
I found a solution (don't if it is a good solution but seems to work) using a list pass to the context instead of using serializerdata = json.dumps(list(Medicament.objects.filter(med_ide__lte=10).values('med_ide','med_num','med_dru')))
我可以使用values()方法提取所需的字段
I can use values() method to extract fields I need
这篇关于JSON-Django/Webdatarocks:无法正确序列化JSON中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!