Example #1
0
    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
Example #2
0
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'
Example #3
0
    class TestSerializer(PeeWeeModelSerializer):
        extras = MethodField()

        class Meta:
            model = ModelA
            fields = ['name', 'extras']

        def get_extras(self, obj):
            return 'patrick star'
Example #4
0
    class ASerializer(Serializer):
        foo = StrField(label='spongebob')
        bar = MethodField(label='patrick')

        def get_bar(self, obj):
            return obj.bar
Example #5
0
    class ASerializer(Serializer):
        a = MethodField()

        def get_a(self, obj):
            return obj.a
Example #6
0
    class ASerializer(Serializer):
        a = MethodField(required=False)

        def get_a(self, obj):
            return obj.a