1

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", 
        ....
     }
]

enter image description here

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?

thorn0
  • 9,362
  • 3
  • 68
  • 96
SLATER
  • 613
  • 9
  • 31

2 Answers2

1

you must get fields value from json, like data.get('fields') and then send it in context.

  • thansk for replying. I try data = serializers.serialize("json", Medicament.objects.filter(med_ide__lte=10)).get('med_dru','med_num') but I get an error 'str' object has no attribute 'get' – SLATER Jan 16 '20 at 12:31
  • I also try values method to extract part I am interested on : data = serializers.serialize("json", Medicament.objects.filter(med_ide__lte=10).values('med_ide','med_num','med_dru')) but get an other error 'dict' object has no attribute '_meta' – SLATER Jan 16 '20 at 12:32
  • I controle type of data like this and it is a QuerySet – SLATER Jan 16 '20 at 12:33
  • https://repl.it/repls/WorldlyPushyLicense you can manipulate with dict like this – Luka Tikaradze Jan 16 '20 at 14:00
  • object return with serializers.serialize is not a list so that your solution do not work; I try to cast into list but the same – SLATER Jan 27 '20 at 17:21
1

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

SLATER
  • 613
  • 9
  • 31