Last active
May 24, 2019 14:09
-
-
Save michaelhelmick/962fe8e54a79d8df0752fc12075e1866 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 Photo(models.Model): | |
thumbnail = models.ImageField(...) | |
... | |
def get_small_thumbnail(self): | |
return get_thumbnail(size='small') | |
def get_medium_thumbnail(self): | |
return get_thumbnail(size='medium') | |
@property | |
def large_thumbnail(self): | |
return get_thumbnail(size='large') |
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 SinglePhotoSerializer(serializers.Serializer): | |
small = DynamicMethodCharField(source='get_small_thumbnail', required=False, read_only=True) | |
medium = DynamicMethodCharField(source='get_medium_thumbnail', required=False, read_only=True) | |
large = CharField(source='large_thumbnail', required=False, 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