Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ambsw-technology
Copy link

When I separated the methods of the HalModelSerializer into a class that was not inherited from ModelSerializer, it appears to work correctly for my case (i.e. #112).

… 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.
Copy link
Contributor

@webjunkie webjunkie left a 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 Show resolved Hide resolved
@@ -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
Copy link
Contributor

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.

ambsw-technology and others added 2 commits February 27, 2020 11:53
Class inherits so it's not a Mixin.

Co-Authored-By: Julian Bez <[email protected]>


class HalModelSerializer(HalSerializerMixin, HyperlinkedModelSerializer):
"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

@@ -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)

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.

@@ -143,3 +141,11 @@ class Meta:
field_kwargs = get_nested_relation_kwargs(relation_info)

return field_class, field_kwargs


class HalModelSerializer(HalSerializerMixin, HyperlinkedModelSerializer):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

@@ -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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

@@ -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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants