From 81097520619aef6cd001f85d8bd5ec133a9e20df Mon Sep 17 00:00:00 2001 From: Erick Wilder Date: Wed, 17 Feb 2016 10:52:21 +0100 Subject: [PATCH 1/3] Closes #3937. Support callable as the value of `initial` for any `serializer.Field` --- rest_framework/fields.py | 2 ++ tests/test_fields.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 6d5962c8ec..917e412cc4 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -370,6 +370,8 @@ def get_initial(self): Return a value to use when the field is being returned as a primitive value, without any object instance. """ + if callable(self.initial): + return self.initial() return self.initial def get_value(self, dictionary): diff --git a/tests/test_fields.py b/tests/test_fields.py index 43441c2e7c..0a878ca790 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -191,6 +191,24 @@ def test_initial(self): } +class TestInitialWithCallable: + def setup(self): + def initial_value(): + return 123 + + class TestSerializer(serializers.Serializer): + initial_field = serializers.IntegerField(initial=initial_value) + self.serializer = TestSerializer() + + def test_initial_should_accept_callable(self): + """ + Follows the default ``Field.initial`` behaviour where they accept a + callable to produce the initial value""" + assert self.serializer.data == { + 'initial_field': 123, + } + + class TestLabel: def setup(self): class TestSerializer(serializers.Serializer): From 908efd665699aedd11878fecaa094c263cf9f457 Mon Sep 17 00:00:00 2001 From: Erick Wilder Date: Wed, 17 Feb 2016 11:01:09 +0100 Subject: [PATCH 2/3] #3937 Update docs for the Field.initial attribute. Add an usage example on how to pass a callable to the `Field.initial` in the docs. --- docs/api-guide/fields.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 5acaf123ec..a9bbabc89f 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -81,7 +81,13 @@ A text string that may be used as a description of the field in HTML form fields ### `initial` -A value that should be used for pre-populating the value of HTML form fields. +A value that should be used for pre-populating the value of HTML form fields. You may pass a callable to it, just as +you may do with any regular Django `Field`: + + def user_default_color(): + return 'blue' + + color = serializers.CharField(initial=user_default_color) ### `style` From a32baeab0c4324989f0d7f17b934a671f2537105 Mon Sep 17 00:00:00 2001 From: Erick Wilder Date: Fri, 19 Feb 2016 09:53:40 +0100 Subject: [PATCH 3/3] Use datetime example for the `Field.initial` attribute Just like regular Django fields, the example adopted uses a `datetiume.date.today` to illustrate the functionality. --- docs/api-guide/fields.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index a9bbabc89f..0409f9a6c6 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -84,10 +84,10 @@ A text string that may be used as a description of the field in HTML form fields A value that should be used for pre-populating the value of HTML form fields. You may pass a callable to it, just as you may do with any regular Django `Field`: - def user_default_color(): - return 'blue' - - color = serializers.CharField(initial=user_default_color) + import datetime + from rest_framework import serializers + class ExampleSerializer(serializers.Serializer): + day = serializers.DateField(initial=datetime.date.today) ### `style` pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy