I don’t understand why code like this give “has_children” attribute to every item object:
items = Items.objects.filter(user=request.user).filter(parent=None)
for i in items:
if i.get_children():
i.has_children = True
else:
i.has_children = False
return render_to_response('items_base.html', {'items': items}, context_instance=RequestContext(request))
But code like this, don’t do the trick, even if it don’t raise any errors:
response = HttpResponse()
response['ContentType'] = "text/javascript"
try:
items = Items.objects.get(id=id).get_children()
for i in items:
if i.get_children():
i.has_children = True
else:
i.has_children = False
response.write(serializers.serialize("json", items))
return response
except ObjectDoesNotExist:
return HttpResponse(u"There is no page like this")
Has someone has any suggestions of what is wrong with secuond code?
Django serialization is excluding any attributes which were not actually defined as model fields.
You have some options:
{fieldname: value}and use thejsonlibrary to serialize them (or create a custom JSONEncoder).