[Answer]-How to build a django query to save mixing int and str from a json var?

1👍

I am not so involved in django, but it seems to me that you do not initialize your Model correctly. From the django documenation:

Creating objects

To create a new instance of a model, just instantiate it like any other Python class:

class Model(**kwargs)

The keyword arguments are simply the names of the fields you’ve defined on your model.

So I would expect you’d rather have to build one dict from your json and than use this as your kwargs for the Model-init:

myjson = [{"name":"bla", "value":111}, 
          {"name":"ble", "value":222}, 
          {"name":"bli", "value":333}, 
          {"name":"blo", "value":444}]

kwargs = dict(map(lambda x: (x["name"], x["value"]), myjson))    
kwargs["name"] = "lalala"

table = myTable(**kwargs)
table.save()

BTW, I renamed query to table; you do not create a query, you create an object (instance of class myTable). This object then takes care of creating the query for the database when you call save()

Greetings,
Thorsten

0👍

@Thorsten: Yes it worked out for me. Here the snippet (directly from my views.py)

mydict = dict(map(lambda x: (x["name"], x["value"]), myjson)) 
mydict["query"]="lalala" 
m = Queries(**mydict) 
m.save()

It get save correctly in the ddbb. Thanks again

Leave a comment