-
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
Conversation
… that was not inherited from `ModelSerializer`, it appears to have worked correctly for my other case. I will try to add test cases, but wanted to at least get it out there in case someone else has the need.
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.
Thanks for the contribution. I did a quick review.
drf_hal_json/serializers.py
Outdated
@@ -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 |
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.
Class inherits so it's not a Mixin. Co-Authored-By: Julian Bez <[email protected]>
|
||
|
||
class HalModelSerializer(HalSerializerMixin, HyperlinkedModelSerializer): | ||
""" |
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.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
@@ -56,7 +52,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 comment
The 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.
drf_hal_json/serializers.py
Outdated
@@ -143,3 +141,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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
@@ -133,7 +131,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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
@@ -99,7 +97,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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
@@ -72,7 +68,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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
@@ -56,7 +52,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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
drf_hal_json/serializers.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
undefined name 'HalSerializerMixin'
When I separated the methods of the
HalModelSerializer
into a class that was not inherited fromModelSerializer
, it appears to work correctly for my case (i.e. #112).