Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter read_only+default behaviour #5886

Merged
merged 2 commits into from Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/api-guide/validators.md
Expand Up @@ -189,7 +189,6 @@ A default class that can be used to *only set a default argument during create o
It takes a single argument, which is the default value or callable that should be used during create operations.

created_at = serializers.DateTimeField(
read_only=True,
default=serializers.CreateOnlyDefault(timezone.now)
)

Expand Down
3 changes: 1 addition & 2 deletions rest_framework/serializers.py
Expand Up @@ -367,8 +367,7 @@ def fields(self):
@cached_property
def _writable_fields(self):
return [
field for field in self.fields.values()
if (not field.read_only) or (field.default is not empty)
field for field in self.fields.values() if not field.read_only
]

@cached_property
Expand Down
9 changes: 8 additions & 1 deletion tests/test_fields.py
Expand Up @@ -219,10 +219,17 @@ def example_callable(self):
class TestReadOnly:
def setup(self):
class TestSerializer(serializers.Serializer):
read_only = serializers.ReadOnlyField()
read_only = serializers.ReadOnlyField(default="789")
writable = serializers.IntegerField()
self.Serializer = TestSerializer

def test_writable_fields(self):
"""
Read-only fields should not be writable, even with default ()
"""
serializer = self.Serializer()
assert len(serializer._writable_fields) == 1

def test_validate_read_only(self):
"""
Read-only serializers.should not be included in validation.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serializer.py
Expand Up @@ -513,7 +513,7 @@ class ExampleSerializer(serializers.Serializer):
class TestDefaultInclusions:
def setup(self):
class ExampleSerializer(serializers.Serializer):
char = serializers.CharField(read_only=True, default='abc')
char = serializers.CharField(default='abc')
integer = serializers.IntegerField()
self.Serializer = ExampleSerializer

Expand Down