I'm assuming that the kind of data that you want to go in the 'client' field is some kind of string/id. I so, you should return the exact field that contains that string. Eg.: `token.client.id` If what you want in the field is a serialized object, you should write a new serializer to it and explicitly serialize it in the method. (in this case I don't know how it was working before)
On Thu, Jan 26, 2017 at 6:07 PM, Luke Wilk <[email protected]> wrote: > For a simple ModelSerializer, the response JSON contains instances of > models, which fails when the response is JSON serialized. For example: > > class AccountSerializer(ModelSerializer): > tokens = SerializerMethodField() > > def get_tokens(self, obj): > token_list = [] > for token in obj.get_tokens(): > token_list.append({ > 'client' : token.client, # <----- token.client is an > instance of a model > 'account_id' : token.account_id > }) > return token_list > > The response fails with the following error: > TypeError: <client object> is not JSON serializable > > This was perfectly valid in rest framework 2.x, as the result of the > get_tokens method would then be automatically serialized further. > > I'm wondering, is there an easy way to upgrade for this type of expected > behavior to DRF v3, or can I simply not rely on code like this and > explicitly serialize objects? As an aside, I've noticed instances of models > in the result of ModelSerializer calls where the ModelSerializer was > serializing anything more than depth=1. > > After extensive searching, I have not found another issue like this one > and so any help would be appreciated. > Thank you > > -- > You received this message because you are subscribed to the Google Groups > "Django REST framework" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- *Filipe Ximenes* Partner & Developer Hangout: [email protected] Skype: filipeaximenes We design, develop and fix beautiful software. *http://www.vinta.com.br/ <http://www.vinta.com.br/>* -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
