-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-Model Serializer #115
base: master
Are you sure you want to change the base?
Non-Model Serializer #115
Changes from 1 commit
5fea149
07e6e9e
895a94d
1ebd9e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from rest_framework.serializers import BaseSerializer, HyperlinkedModelSerializer, ListSerializer | ||
from rest_framework.utils.field_mapping import get_nested_relation_kwargs | ||
from rest_framework.utils.serializer_helpers import ReturnDict | ||
from rest_framework.serializers import Serializer | ||
|
||
|
||
class HalListSerializer(ListSerializer): | ||
|
@@ -30,16 +31,12 @@ def data(self): | |
) | ||
|
||
|
||
class HalModelSerializer(HyperlinkedModelSerializer): | ||
""" | ||
Serializer for HAL representation of django models | ||
""" | ||
serializer_related_field = HyperlinkedRelatedField | ||
serializer_url_field = HalHyperlinkedIdentityField | ||
class HalSerializerMixin(Serializer): | ||
ambsw-technology marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
default_list_serializer = HalListSerializer | ||
|
||
def __init__(self, instance=None, data=empty, **kwargs): | ||
super(HalModelSerializer, self).__init__(instance, data, **kwargs) | ||
super(HalSerializerMixin, self).__init__(instance, data, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
self.nested_serializer_class = self.__class__ | ||
if data != empty and not LINKS_FIELD_NAME in data: | ||
data[LINKS_FIELD_NAME] = dict() # put links in data, so that field validation does not fail | ||
|
@@ -56,7 +53,7 @@ class Meta: | |
list_serializer_class = getattr(meta, 'list_serializer_class', None) | ||
if list_serializer_class is None: | ||
setattr(meta, 'list_serializer_class', cls.default_list_serializer) | ||
return super(HalModelSerializer, cls).many_init(*args, **kwargs) | ||
return super(HalSerializerMixin, cls).many_init(*args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not call setattr with a constant attribute value, it is not any safer than normal property access. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
|
||
def build_link_object(self, val): | ||
if (type([]) == type(val)): | ||
|
@@ -72,7 +69,7 @@ def _get_url(self, item): | |
return None | ||
|
||
def to_representation(self, instance): | ||
ret = super(HalModelSerializer, self).to_representation(instance) | ||
ret = super(HalSerializerMixin, self).to_representation(instance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
resp = defaultdict(dict) | ||
|
||
for field_name in self.link_field_names: | ||
|
@@ -87,6 +84,8 @@ def to_representation(self, instance): | |
for field_name in self.embedded_field_names: | ||
# if a related resource is embedded, it should still | ||
# get a link in the parent object | ||
if field_name not in ret: | ||
continue | ||
if type(ret[field_name]) == list: | ||
embed_self = list(filter(lambda x: x is not None, [self._get_url(x) for x in ret[field_name] if x])) | ||
else: | ||
|
@@ -99,7 +98,7 @@ def to_representation(self, instance): | |
return resp | ||
|
||
def get_fields(self): | ||
fields = super(HalModelSerializer, self).get_fields() | ||
fields = super(HalSerializerMixin, self).get_fields() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
|
||
self.embedded_field_names = [] | ||
self.link_field_names = [] | ||
|
@@ -133,7 +132,7 @@ def build_nested_field(self, field_name, relation_info, nested_depth): | |
""" | ||
Create nested fields for forward and reverse relationships. | ||
""" | ||
class NestedSerializer(HalModelSerializer): | ||
class NestedSerializer(HalSerializerMixin): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
class Meta: | ||
model = relation_info.related_model | ||
depth = nested_depth - 1 | ||
|
@@ -143,3 +142,11 @@ class Meta: | |
field_kwargs = get_nested_relation_kwargs(relation_info) | ||
|
||
return field_class, field_kwargs | ||
|
||
|
||
class HalModelSerializer(HalSerializerMixin, HyperlinkedModelSerializer): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undefined name 'HalSerializerMixin' |
||
Serializer for HAL representation of django models | ||
""" | ||
serializer_related_field = HyperlinkedRelatedField | ||
serializer_url_field = HalHyperlinkedIdentityField |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this to the existing
serializers
imports.