Reputation: 3161
This is coming from a request to get all objects of a Django apps, it is not getting a plain object, as the print says it is just a string
Javascript :
$.getJSON("/cadastro/getAllPessoas/", function(data){
console.log(data);
console.log(typeof(data));
console.log($.isPlainObject(data));
//Raises error on isArrayLike():
$.each(data,function(){
arrayValues.push([this["pk"],this["fields"]["nome"]]);
})
});
Console output :
[{"model": "cadastroapp.djangotestpessoa", "pk": 1, "fields": {"nome": "Gabriel"}}]
string
false
views.py :
from django.core import serializers
def getAllPessoas(request):
data = serializers.serialize('json', Pessoa.objects.all(), fields=('objectid','nome'))
return JsonResponse(data, safe=False)
Upvotes: 0
Views: 157
Reputation: 599580
You're serializing twice in the Django view, because both serializers.serialize
and JsonResponse convert to JSON. Don't do that; just return a normal response with the serialized value.
return HttpResponse(data, content_type='application/json')
Upvotes: 3