1

Client get data combined from two tables. Here is the code from the file views.py:

...
dbdataA = TableA.objects.filter(user=user)
dbdataB = TableB.objects.filter(user=user).order_by("field1")
result_list = list(chain(orders, alldata))
msg = serializers.serialize("json", result_list, fields=("field1","field2","field3","field4"), use_natural_keys=True)
return HttpResponse(msg)

and I need to add a var with current time to response. How to do this?

UPD: Any data passed to chain(orders, alldata, ...) gives error " 'str' object have no attribute '_meta' " in string " msg =serializers.serialize...". Maybe it's because chain() can't take any data except iterable, but (time.asctime(time.localtime()) is not iterable?

kvadrat
  • 135
  • 2
  • 11

1 Answers1

2

Add the variable to your result_list and your fields list.

import time
result_list = list(chain(orders, alldata,
                         (time.asctime(time.localtime()),))
                  ) #or other time function
msg = serializers.serialize("json", result_list,
                            fields=("field1", "field2", "field3", "field4", "time"),
                            use_natural_keys=True)

Or, if you don't want to add the time to the JSON object, HTTPResponse(msg) already sets the Date parameter in the Http Response header.

Dave
  • 2,396
  • 2
  • 22
  • 25
  • 1
    Date parameter in the Http Response header it's a good solution. Also I found some about it here http://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header/4236041#4236041 – kvadrat Oct 17 '13 at 16:39