def test_method_field(): class MethodSerializer(object): def get_foo(self): return 'bar' serializer = MethodSerializer() field = MethodField(attr='foo') assert field.as_getter('foo', serializer)() == 'bar'
class ASerializer(Serializer): a = MethodField() b = MethodField('add_9') def get_a(self, obj): return obj.a + 5 def add_9(self, obj): return obj.a + 9
class TestSerializer(PeeWeeModelSerializer): extras = MethodField() class Meta: model = ModelA fields = ['name', 'extras'] def get_extras(self, obj): return 'patrick star'
class ASerializer(Serializer): foo = StrField(label='spongebob') bar = MethodField(label='patrick') def get_bar(self, obj): return obj.bar
class ASerializer(Serializer): a = MethodField() def get_a(self, obj): return obj.a
class ASerializer(Serializer): a = MethodField(required=False) def get_a(self, obj): return obj.a