views:

42

answers:

1

Hi, I want to serialize a QuerySet that contains an extra statement:

region_list = Region.objects.extra(select={ 'selected': 'case when id = %s then 1 else 0 end' % (new_region.id)}).all()

I use the statement below to serialize

return HttpResponse(serializers.serialize('json', region_list), mimetype='application/json')

But when I obtain the json results in the browser, only the fields of the Region model appears, the selected field dissapear.

How can I fix that?

A: 

One slightly longwinded solution would be to to dump the objects to JSON via django-piston's JSONEmitter class. When you register your Region model with piston, you can say what fields to include, and mention 'selected' there, and then use your annotation to make sure that the queryset used in the piston handler contains all the info you want.

Or just look at how piston does it and, if you don't want all of piston, just mimic the bits you do.

stevejalim