From 66ae19229e8e0d1f99c21501e821facfb5d0566d Mon Sep 17 00:00:00 2001 From: James Beith Date: Mon, 10 Aug 2015 12:38:27 +0100 Subject: [PATCH] Add docs for `display_value` --- docs/api-guide/relations.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/api-guide/relations.md b/docs/api-guide/relations.md index bc24667de..ba3f88063 100644 --- a/docs/api-guide/relations.md +++ b/docs/api-guide/relations.md @@ -537,6 +537,14 @@ If you explicitly specify a relational field pointing to a ``ManyToManyField`` with a through model, be sure to set ``read_only`` to ``True``. +## The `display_value` method + +The `__str__` (`__unicode__` on Python 2) method of the model will be called to generate string representations of the objects used to populate the `choices` property. To provide customized representations, override `display_value` of a `RelatedField` subclass. This method will receive a model object, and should return a string suitable for representing it. For example: + + class TrackPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): + def display_value(self, instance): + return 'Track: %s' % (instance.title) + --- # Third Party Packages