django object is not iterable using serializers.serialize -


i'm getting following error,

template' object not iterable

def get_ajax(request, id):     data = serializers.serialize("json", template.objects.get(pk=id))     return httpresponse(data) 

however, i'm using 'get' don't understand why i'm getting error. ideas?

that's because you're not passing iterable nor queryset, you're passing instead template object. if want serialize single object can this:

def get_ajax(request, id):     data = serializers.serialize("json", [template.objects.get(pk=id)])     return httpresponse(data) 

update: recommending using filter instead.

also consider using filter instead of in order avoid possible exceptions if pk doesn't exists. way don't need brackets because queryset object

def get_ajax(request, id):     data = serializers.serialize("json", template.objects.filter(pk=id))     return httpresponse(data) 

hope helps!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -