Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/api-guide/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have an example that makes a bit more sense.
I can only think of date/time examples - are there an other obvious use cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the date/time example is fine. Other use cases are possible but I think they'll make it more difficult to the reader to understand. Also, the example from the official Django docs[1] uses date/time too.

[1] https://docs.djangoproject.com/en/1.9/ref/forms/fields/#initial

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great - let's do the same! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @tomchristie
I've updated the docs. May you take a look?


color = serializers.CharField(initial=user_default_color)

### `style`

Expand Down
2 changes: 2 additions & 0 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down