我试图从输出json对象中删除pk
和model
作为Django序列化的一部分。我在views.py中尝试了以下代码,但它抛出了以下错误
TypeError: serialize() takes 2 positional arguments but 3 were given.
为了解决这个问题,我也使用了@staticmethod
,但没有多大用处。
以下是 views.py
def get_stats(request):
if request.method == "POST":
srch_dropV = request.POST['srch_dropAJ']
else:
srch_dropV = ''
if(srch_dropV == 'Lock'):
stud = LockBased.objects.all()
response_data = {}
response_data['result'] = 'Success'
serializer = NewSerializer()
response_data['message'] = serializer.serialize('json', stud)
return HttpResponse(JsonResponse(response_data), content_type="application/json")
from django.core.serializers.json import Serializer, DjangoJSONEncoder
import json
class NewSerializer(Serializer):
def end_serialization(self):
cleaned_objects = []
for obj in self.objects:
del obj['pk']
del obj['model']
cleaned_objects.append(obj)
json.dump(cleaned_objects, self.stream, cls=DjangoJSONEncoder, **self.options)
请帮我确定问题并加以解决 我正在使用Django 1.8和Python 3.4。
答案 0 :(得分:0)
我认为您将var marker = [{43.000,-79.321},...];
var result = [];
function test(){
for(var i=0; i<marker.length;i++){
result[i] =[];
for(var j=0; j<marker.length;j++){
drivingDistance(marker[i],marker[j],i,j,function(cb){
result[cb.i][cb.j] = cb.distance;
});
}
}
}
function drivingDistance(Point1,Point2,i,j,cb){
cb({distance:'something',i:i,j:j});
}
与serializers.serialize()
混为一谈:
serializer.serialize()
与模块级函数相比,序列化程序的方法不采用目标格式参数,而只采用查询集:
serializer = NewSerializer()
response_data['message'] = serializer.serialize('json', stud)