Scott Stafford
Scott Stafford

Reputation: 44798

Can I extend Tastypie to support Django/Postgres ArrayField?

I am using Tastypie and djorm_pgarray.fields.ArrayField (http://www.craigkerstiens.com/2012/11/06/django-and-arrays/).

Tastypie naturally serializes it as a string like this:

my_array_field: "[u'Red', u'Blue', u'Yellow']"

But I want it to look and act like an array, not a string.

Is tastypie extensible in that way? How? Ideally, it would be a centralized, global fix that extends the json serializer to support the ArrayField type.

Upvotes: 1

Views: 231

Answers (2)

elynch
elynch

Reputation: 2240

You don't necessarily have to dehydrate and hydrate the field yourself. Tastypie comes with a ListField which will do it for you. So you could do something like this:

mylist = fields.ListField(attribute='mylist')

Upvotes: 1

Scott Stafford
Scott Stafford

Reputation: 44798

Currently, the best answer I've found is to do this per-field:

def dehydrate_my_array_field(self, bundle):
    return bundle.obj.my_array_field

This will at least give me the array back.

I am not using hydrate, yet.

Upvotes: 1

Related Questions