问题
I currently develop a Django project and try to implement WebDataRocks WebDataRocks is a free web reporting tool for data analysis and visualization
I works but my problem deal with correctly presenting data to be updated in Webdatarocks I would like to update each of my models.
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})
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",
....
}
]
The only 2 variables I get access in webdatarocks table are myapp.mymodel and pk I try to extract only part of my data I need (=fields) using things like data['fields'] but it is not the right syntax
what is wrong?
回答1:
you must get fields value from json, like data.get('fields') and then send it in context.
回答2:
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 serializer data = json.dumps(list(Medicament.objects.filter(med_ide__lte=10).values('med_ide','med_num','med_dru')))
I can use values() method to extract fields I need
来源:https://stackoverflow.com/questions/59768702/json-django-webdatarocks-unable-to-correctly-serialize-data-in-json