Server-side Django queryset extension
Server-side Django queryset extension
Hi, I'm using DataTables 1.82 with Django for a server-side processed table. Sticking with the django example given in this site (i.e. http://www.assembla.com/code/datatables_demo/subversion/nodes/trunk/1_6_2/datatables_demo), had I wanted to extend the queryset being returned to the datatable request with additional (row-related) data (for example 'flag colour' retrieved from a different django model/queryset), how is this best done, i.e. can I do this all in one go on the server side? I had tried the following (which I realise isn't great Django) but this fires the "DataTables warning: Added data does not match known number of columns" warning (debugging the script shows one less aData.length than aoColumns.length):
[code]
def get_countries_list(request):
#prepare the params
#initial querySet
querySet = Country.objects.all()
#----- my code addition --------
for query in querySet:
query.flagcolour = str(Flag.objects.get[country=.............] ....) #essentially saving a string value here
#columnIndexNameMap is required for correct sorting behavior
columnIndexNameMap = { 0: 'id', 1 : 'name', 2: 'formal_name', 3: 'capital', 4: 'currency_code', 5: 'currency_name', 6: 'phone_prefix', 7: 'tld', 8: 'flagcolour }
#path to template used to generate json (optional)
#jsonTemplatePath = 'demo/json_countries.txt'
#call to generic function from utils
return get_datatables_records(request, querySet, columnIndexNameMap, jsonTemplatePath)
[/code]
I've included the extra { "bSearchable": false}, within my page script as well as "bProcessing": true and "bServerSide": true, as well as the extra block within the table html.
Can anyone tell me if I am way off in this approach and whether I should be doing this differently, e.g using one of the callbacks? And if not, are there suggestions to how to correctly add this extra info to my returned data.
Any help is very much appreciated. And if necessary I can include more detailed code snippets of my actual code - I just used the example above to get across what I'm doing.
regards,
G
[code]
def get_countries_list(request):
#prepare the params
#initial querySet
querySet = Country.objects.all()
#----- my code addition --------
for query in querySet:
query.flagcolour = str(Flag.objects.get[country=.............] ....) #essentially saving a string value here
#columnIndexNameMap is required for correct sorting behavior
columnIndexNameMap = { 0: 'id', 1 : 'name', 2: 'formal_name', 3: 'capital', 4: 'currency_code', 5: 'currency_name', 6: 'phone_prefix', 7: 'tld', 8: 'flagcolour }
#path to template used to generate json (optional)
#jsonTemplatePath = 'demo/json_countries.txt'
#call to generic function from utils
return get_datatables_records(request, querySet, columnIndexNameMap, jsonTemplatePath)
[/code]
I've included the extra { "bSearchable": false}, within my page script as well as "bProcessing": true and "bServerSide": true, as well as the extra block within the table html.
Can anyone tell me if I am way off in this approach and whether I should be doing this differently, e.g using one of the callbacks? And if not, are there suggestions to how to correctly add this extra info to my returned data.
Any help is very much appreciated. And if necessary I can include more detailed code snippets of my actual code - I just used the example above to get across what I'm doing.
regards,
G
This discussion has been closed.