1

客户端从两个表中获取数据。这是文件中的代码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)

我需要添加一个具有当前响应时间的变量。这个怎么做?

UPD: 传递给链(订单,所有数据,...)的任何数据都会在字符串“msg =serializers.serialize...”中给出错误“'str' object has no attribute '_meta'”。可能是因为chain()除了iterable之外不能取任何数据,但是(time.asctime(time.localtime())是不可迭代的?

4

1 回答 1

2

将变量添加到您result_list和您的fields列表中。

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)

或者,如果您不想将时间添加到 JSON 对象,则HTTPResponse(msg) 已经在 Http Response 标头中设置了 Date 参数。

于 2013-10-16T20:03:06.380 回答