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 -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -