How customize serializers to not include field names?

256 views
Skip to first unread message

Rex Posadas

unread,
Dec 21, 2014, 11:26:18 PM12/21/14
to django-res...@googlegroups.com
Hi All, 

I created a StackOverflow question for this but with no luck : http://stackoverflow.com/questions/27364211/how-to-serialize-models-without-field-names-in-the-json-output

I thought it needed to be in this group for gain some traction. 

I was reading through overriding the serializer behavior by following this: http://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior

I was not able to get it to work. Does anyone have any ideas on how I can accomplish serializing a model without using the field names?  More to the point, does anyone have any examples I can look at?

Thanks, 
Rex

Tom Christie

unread,
Dec 22, 2014, 6:57:52 PM12/22/14
to django-res...@googlegroups.com
Because the style you want doesn't map well to the standard Serializer field output you're going to want to simply override '.to_representation' on the serializer completely. That'll allow you to handle each instance however you want, you simply get that the instance as a parameter to that method, and return whatever primitive data structure you want. You may as well drop down to using 'BaseSerializer' in that case as it'll be easier to see how the code works and you won't be getting any benefit from using the 'Serializer' class at that point.

Rex Posadas

unread,
Dec 24, 2014, 9:41:29 AM12/24/14
to django-res...@googlegroups.com
Awesome. Thank you for pointing me in the right direction Tom. 

Rex Posadas

unread,
Jan 5, 2015, 2:18:28 AM1/5/15
to django-res...@googlegroups.com
I am getting the following error when I simply override '.to_representation' and drop down to using 'BaseSerilaizer'. 

'SettingSerializer' object has no attribute 'base_fields'

SettingSerializer is the name of my custom serializer. 

So I have this: 
class SettingSerializer(serializers.BaseSerializer):

  def to_representation(self, instance):
  ret = OrderedDict()
  return ret

  class Meta:
  model = Setting
  fields = ('name', 'value')


I can't seem to find where in the code that error is thrown.  When using the BaseSerializer, do I need to override more than just the 'to_representation()' method?

Thanks.


On Monday, December 22, 2014 5:57:52 PM UTC-6, Tom Christie wrote:

Tom Christie

unread,
Jan 5, 2015, 5:06:08 AM1/5/15
to django-res...@googlegroups.com
Rex, could you show us the traceback - it's possible that there's an issue that needs resolving.


> When using the BaseSerializer, do I need to override more than just the 'to_representation()' method?

You shouldn't need to, no.

Tom Christie

unread,
Jan 5, 2015, 10:05:52 AM1/5/15
to django-res...@googlegroups.com
You might want to double check that you're using the latest version (3.0.2).
`base_fields` is not referenced anywhere in the REST framework codebase at this point (apart from an erronous docstring that I've now fixed)

Rex Posadas

unread,
Jan 5, 2015, 12:42:27 PM1/5/15
to django-res...@googlegroups.com
Thank you for taking the time to look at this Tom. 

I am using DRF 2.4.3 since it’s what’s compatible with a GIS package I am using.


I tried going up to DRF 3.0.2 and, as expected, experienced compatibility problems. 

Should I look for an alternative solution to my problem? if so, can you point me to the right direction?

Thanks, 
Rex

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/k-T-C_5O2SA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Christie

unread,
Jan 6, 2015, 8:31:53 AM1/6/15
to django-res...@googlegroups.com
Sorry not to be more help, but I'm not really in a position to help out with 2.x issues as that'd mean dropping time & effort from ongoing 3.x work.
I'd be happy to help spend time on any -> 3 migration issues and discuss those on the mailing list where it might also help other folks or highlight issues we should be focussing on.

Rex Posadas

unread,
Jan 6, 2015, 10:16:50 AM1/6/15
to django-res...@googlegroups.com
That’s perfectly fine.  I completely understand.  



On Jan 6, 2015, at 7:31 AM, Tom Christie <christ...@gmail.com> wrote:

Sorry not to be more help, but I'm not really in a position to help out with 2.x issues as that'd mean dropping time & effort from ongoing 3.x work.
I'd be happy to help spend time on any -> 3 migration issues and discuss those on the mailing list where it might also help other folks or highlight issues we should be focussing on.

Anthony Lalande

unread,
Nov 17, 2015, 1:18:53 AM11/17/15
to Django REST framework
I found this thread when I ran into the same need to create a custom serializer in DRF (2.3.x). I got it to work by making two very simple tweaks to the solution posted by Tom Christie above, and wanted to share for anyone else.

1. The serializer class simply needs a `base_fields = {}` attribute in order to successfully initialize.
2. The serialization method in DRF 2.x is actually `to_native()`, not `to_represenation()`. It will be called with the instance of the Python object to serialize.


In essence, I believe all you need is:

```
from rest_framework import serializers


class GraphSerializer(serializers.BaseSerializer):
    base_fields = {}

    def to_native(obj):
        return obj.subgraph_to_dict()
```

Hope this helps!
- Anthony
Reply all
Reply to author
Forward
0 new messages