1 from rest_framework import serializers
2 from django.core.urlresolvers import reverse
5 class AbsoluteURLField(serializers.ReadOnlyField):
6 def __init__(self, view_name=None, view_args=None, source='get_absolute_url', *args, **kwargs):
7 if view_name is not None:
9 super(AbsoluteURLField, self).__init__(*args, source=source, **kwargs)
10 self.view_name = view_name
14 fields = v.split(':', 1)
15 self.view_args[fields[0]] = fields[1] if len(fields)>1 else fields[0]
17 def to_representation(self, value):
18 if self.view_name is not None:
20 arg: getattr(value, field)
21 for (arg, field) in self.view_args.items()
23 value = reverse(self.view_name, kwargs=kwargs)
24 return self.context['request'].build_absolute_uri(value)
27 class LegacyMixin(object):
28 def to_representation(self, value):
29 value = super(LegacyMixin, self).to_representation(value)
30 non_null_fields = getattr(getattr(self, 'Meta', None), 'legacy_non_null_fields', [])
31 for field in non_null_fields:
32 if field in value and value[field] is None: