From daa6c385ff0cb920d061d3a593a27c08176e1181 Mon Sep 17 00:00:00 2001 From: Paul Wayper Date: Mon, 8 Oct 2018 10:17:08 +1100 Subject: [PATCH] Document setting the type of a SerializerMethodField --- docs/api-guide/fields.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 8d25d6c78..0bd1d3b93 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -562,6 +562,19 @@ The serializer method referred to by the `method_name` argument should accept a --- +By default, `SerializerMethodField`s are assumed to return a string. If your method returns something else, and you want your OpenAPI schema to be consistent, you should create a custom class that inherits from both `SerializerMethodField` and the other field. For example: + + from rest_framework import serializers + + class IntegerMethodField(serializers.SerializerMethodField, serializers.IntegerField): + pass + + class UserSerializer(serializers.ModelSerializer): + days_since_joined = IntegerMethodField() + .... + +--- + # Custom fields If you want to create a custom field, you'll need to subclass `Field` and then override either one or both of the `.to_representation()` and `.to_internal_value()` methods. These two methods are used to convert between the initial datatype, and a primitive, serializable datatype. Primitive datatypes will typically be any of a number, string, boolean, `date`/`time`/`datetime` or `None`. They may also be any list or dictionary like object that only contains other primitive objects. Other types might be supported, depending on the renderer that you are using.