Last active
May 24, 2019 14:03
-
-
Save michaelhelmick/5355fdd06dfc810c275db4a5f84721ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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 DynamicMethodField(serializers.Field): | |
"""Field that accepts instance methods and method kwargs.""" | |
def __init__(self, *args, **kwargs): | |
"""Extend DynamicMethodCharField.__init__. | |
Args: | |
method (str): Method on the instance to call. | |
method_args (list): List of arguments to pass to the | |
instance method. | |
method_kwargs (dict): Dictionary of keyword arguments to | |
pass to the instance method. | |
Raises: | |
Exception: If method is not passed. | |
""" | |
self.method = kwargs.pop('method', None) | |
if not self.method: | |
raise Exception('Please provide a method to this serializer field.') | |
self.method_args = kwargs.pop('method_args', []) | |
self.method_kwargs = kwargs.pop('method_kwargs', {}) | |
super(DynamicMethodField, self).__init__(*args, **kwargs) | |
def to_representation(self, obj): | |
"""Representation of instance method.""" | |
return getattr(obj, self.method)(*self.method_args, **self.method_kwargs) | |
class DynamicMethodCharField(DynamicMethodField, serializers.CharField): | |
"""CharField that accepts instance methods and method kwargs. | |
Example: | |
avatar_sm = DynamicMethodCharField( | |
source='*', method='get_thumbnail', | |
method_kwargs={'size': 'small'}) | |
""" | |
def __init__(self, *args, **kwargs): | |
"""Extend DynamicMethodCharField.__init__.""" | |
super(DynamicMethodCharField, self).__init__(*args, **kwargs) |
This file contains hidden or 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
from rest_framework import serializers | |
from core_serializers import DynamicMethodCharField | |
class SinglePhotoSerializer(serializers.Serializer): | |
small = DynamicMethodCharField( | |
source='*', required=False, method='get_thumbnail', method_kwargs={'size': 'small'}, | |
read_only=True) | |
medium = DynamicMethodCharField( | |
source='*', required=False, method='get_thumbnail', method_kwargs={'size': 'medium'}, | |
read_only=True) | |
large = DynamicMethodCharField( | |
source='*', required=False, method='get_thumbnail', | |
method_kwargs={'size': 'large'}, read_only=True) | |
class Meta: | |
fields = ('small', 'medium', 'large',) | |
class PhotoSerializer(serializers.ModelSerializer): | |
thumbnails = SinglePhotoSerializer(source='*', required=False, read_only=True) | |
class Meta: | |
model = Photo | |
fields = ('thumbnails') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment