From 7a3d96d48a663c4cecbfff2d51889156170407d9 Mon Sep 17 00:00:00 2001 From: Noah Kantrowitz Date: Wed, 8 Dec 2021 19:44:52 -0800 Subject: [PATCH] Default SerializerMethodField's help_text to the docstring of the method. --- rest_framework/fields.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d7e7816ce..eeab9b064 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1878,6 +1878,11 @@ class SerializerMethodField(Field): # The method name defaults to `get_{field_name}`. if self.method_name is None: self.method_name = 'get_{field_name}'.format(field_name=field_name) + # If help_text isn't specified, try to get the method's docstring. + if self.help_text is None: + method = getattr(parent, self.method_name, None) + if method is not None: + self.help_text = method.__doc__ super().bind(field_name, parent)