Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove pk field from django serialized objects

Tags:

I'm serializing a bunch of objects with:

json = serializers.serialize("json", objects, fields=('name', 'country')) 

I have not included 'pk' in my fields list, but the produced JSON includes a primary key with each serialized object. I do not want my API to expose primary keys to the general public.

Short of munging the output JSON, what is the correct way to tell serializers.serialze() to not include the primary key?

like image 529
Josh Reich Avatar asked Oct 23 '09 20:10

Josh Reich


1 Answers

I ended up working around this by 'manually' achieving what I wanted using the underlying simplejson serializer:

from django.utils import simplejson json = simplejson.dumps( [{'name': o.name,                            'country': o.country} for o in objects] ) 

A little more typing, but works.

like image 178
Josh Reich Avatar answered Sep 19 '22 02:09

Josh Reich