Reputation: 1172
I am writing some code so that I can get data from a database using the ajax function on the datatables and I am having a bit of trouble with formatting my dicts so they will look right when converted to json.
What I am trying to do is create a variable with the name data
that will contain all the rows I need to display for this particular method it is all the items I currently have the code bellow it doesn't work I know this is because of mismatched types and not doing a straight assign. What I am aiming for is like a multi layered array in php that is what I need data
to be like I will put an example of formatting bellow my code.
data = None
for self.result in self.result.iterator():
data += { 'Name' : self.result.product, 'Cost' : self.result.cost, 'Keywords' : self.result.keywords, 'ImageID' : self.result.image_id }
json = {'sEcho' : self.request.POST('sEcho'), 'iTotalRecords' : self.count, 'iTotalDisplayRecords' : self.count, 'aaData' : data }
This is the structure example that will work with my datatables I know this because I have done the same thing in php i will show this bellow to further illustrate what I am after in python
{'sEcho' : 1, 'iTotalRecords' : 5, 'iTotalDisplayRecords' : 5, 'aaData' : {{'name' : 'one'},{'name' : 'two'},{ 'name' : 'three'}} }
PHP working format
$this->output = array(
'sEcho' => 1,
'iTotalRecords' => 5,
'iTotalDisplayRecords' => 5,
'aaData' => array( )
);
Upvotes: 0
Views: 80
Reputation: 653
you need to use list inside the data
import json
data = [
{ "name": "john doe", "address": "miami" },
{ "name": "jane doe", "address": "las vegas" },
......
]
value = {
'aaData': data,
'sEcho': 1,
........
}
self.response.write(json.dumps(value))
Upvotes: 1