Created
March 20, 2013 22:37
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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