siteshen
2017-02-28 12:41:56 +08:00
下面我用的一种方式, DRF 应该有类似的自定义机制
```
class UserSerializer(object):
fields = ['username', 'profile', 'is_member']
def serialize_field(self, field):
func = getattr('serialize_field_%s' % field, None)
if func:
return self.func()
return super(UserSerializer, self).serialize_field()
def serialize_field_profile(self):
return {
'private': 'private property',
'public': 'public property',
}
class GuestUserSerializer(UserSerializer):
fields = ['username', 'profile']
def serialize_field_profile(self):
return {'public': 'public property'}
```