Skip to content

Instantly share code, notes, and snippets.

@JustinTArthur
Created March 20, 2013 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinTArthur/5209181 to your computer and use it in GitHub Desktop.
Save JustinTArthur/5209181 to your computer and use it in GitHub Desktop.
A Django model serializer for Django REST Framework that will serialize lists in the format that Ember.js expects in its JSONs. This is not the best way to go about this, but is a very fast way to get your stuff working. The biggest caveat is that if you use one of these serializers nested inside another with many=True, the resulting format will…
class EmberModelSerializer(serializers.ModelSerializer):
"""
A serializer that formats list data in a way that will be readable by Ember.js.
"""
@classmethod
def get_class_ember_name(cls):
type_name = cls.__name__.replace('Serializer','')
return type_name.lower()
@property
def data(self):
class_name = self.get_class_ember_name()
serialized_data = super(EmberModelSerializer, self).data
if self.many:
return {"%ss" % class_name: serialized_data}
else:
return serialized_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment