Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
7a408f6c
Commit
7a408f6c
authored
Feb 22, 2017
by
Ran Benita
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Guard against the possible misspelling `readonly_fields` in model serializers
Fixes #4897.
parent
d82dbc09
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
0 deletions
+25
-0
rest_framework/serializers.py
+9
-0
tests/test_model_serializer.py
+16
-0
No files found.
rest_framework/serializers.py
View file @
7a408f6c
...
...
@@ -1290,6 +1290,15 @@ class ModelSerializer(Serializer):
kwargs
[
'read_only'
]
=
True
extra_kwargs
[
field_name
]
=
kwargs
else
:
# Guard against the possible misspelling `readonly_fields` (used
# by the Django admin and others).
assert
not
hasattr
(
self
.
Meta
,
'readonly_fields'
),
(
'Serializer `
%
s.
%
s` has field `readonly_fields`; '
'the correct spelling for the option is `read_only_fields`.'
%
(
self
.
__class__
.
__module__
,
self
.
__class__
.
__name__
)
)
return
extra_kwargs
def
get_uniqueness_extra_kwargs
(
self
,
field_names
,
declared_fields
,
extra_kwargs
):
...
...
tests/test_model_serializer.py
View file @
7a408f6c
...
...
@@ -10,6 +10,7 @@ from __future__ import unicode_literals
import
decimal
from
collections
import
OrderedDict
import
pytest
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.validators
import
(
MaxValueValidator
,
MinLengthValidator
,
MinValueValidator
...
...
@@ -1064,3 +1065,18 @@ class Issue3674Test(TestCase):
child_expected
=
{
'parent'
:
1
,
'value'
:
'def'
}
self
.
assertEqual
(
child_serializer
.
data
,
child_expected
)
class
Issue4897TestCase
(
TestCase
):
def
test_should_assert_if_writing_readonly_fields
(
self
):
class
TestSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
OneFieldModel
fields
=
(
'char_field'
,)
readonly_fields
=
fields
obj
=
OneFieldModel
.
objects
.
create
(
char_field
=
'abc'
)
with
pytest
.
raises
(
AssertionError
)
as
cm
:
TestSerializer
(
obj
)
.
fields
cm
.
match
(
r'readonly_fields'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment