JSON - Django/Webdatarocks: unable to correctly serialize data in JSON

让人想犯罪 __ 提交于 2020-02-06 07:57:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!