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
654e0e45
Commit
654e0e45
authored
Aug 27, 2015
by
José Padilla
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update ModelSerializer fields behavior
parent
82642224
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
2 deletions
+34
-2
rest_framework/serializers.py
+19
-2
tests/test_model_serializer.py
+15
-0
No files found.
rest_framework/serializers.py
View file @
654e0e45
...
@@ -12,6 +12,8 @@ response content is handled by parsers and renderers.
...
@@ -12,6 +12,8 @@ response content is handled by parsers and renderers.
"""
"""
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
warnings
from
django.db
import
models
from
django.db
import
models
from
django.db.models.fields
import
Field
as
DjangoModelField
from
django.db.models.fields
import
Field
as
DjangoModelField
from
django.db.models.fields
import
FieldDoesNotExist
from
django.db.models.fields
import
FieldDoesNotExist
...
@@ -51,6 +53,8 @@ LIST_SERIALIZER_KWARGS = (
...
@@ -51,6 +53,8 @@ LIST_SERIALIZER_KWARGS = (
'instance'
,
'data'
,
'partial'
,
'context'
,
'allow_null'
'instance'
,
'data'
,
'partial'
,
'context'
,
'allow_null'
)
)
ALL_FIELDS
=
'__all__'
# BaseSerializer
# BaseSerializer
# --------------
# --------------
...
@@ -943,13 +947,13 @@ class ModelSerializer(Serializer):
...
@@ -943,13 +947,13 @@ class ModelSerializer(Serializer):
fields
=
getattr
(
self
.
Meta
,
'fields'
,
None
)
fields
=
getattr
(
self
.
Meta
,
'fields'
,
None
)
exclude
=
getattr
(
self
.
Meta
,
'exclude'
,
None
)
exclude
=
getattr
(
self
.
Meta
,
'exclude'
,
None
)
if
fields
and
not
isinstance
(
fields
,
(
list
,
tuple
)):
if
fields
and
fields
!=
ALL_FIELDS
and
not
isinstance
(
fields
,
(
list
,
tuple
)):
raise
TypeError
(
raise
TypeError
(
'The `fields` option must be a list or tuple. Got
%
s.'
%
'The `fields` option must be a list or tuple. Got
%
s.'
%
type
(
fields
)
.
__name__
type
(
fields
)
.
__name__
)
)
if
exclude
and
not
isinstance
(
exclude
,
(
list
,
tuple
)):
if
exclude
and
exclude
!=
ALL_FIELDS
and
not
isinstance
(
exclude
,
(
list
,
tuple
)):
raise
TypeError
(
raise
TypeError
(
'The `exclude` option must be a list or tuple. Got
%
s.'
%
'The `exclude` option must be a list or tuple. Got
%
s.'
%
type
(
exclude
)
.
__name__
type
(
exclude
)
.
__name__
...
@@ -962,6 +966,19 @@ class ModelSerializer(Serializer):
...
@@ -962,6 +966,19 @@ class ModelSerializer(Serializer):
)
)
)
)
if
fields
is
None
and
exclude
is
None
:
warnings
.
warn
(
"Creating a ModelSerializer without either the 'fields' attribute "
"or the 'exclude' attribute will be prohibited. "
"The {serializer_class} serializer needs updating."
.
format
(
serializer_class
=
self
.
__class__
.
__name__
),
PendingDeprecationWarning
)
if
fields
==
ALL_FIELDS
:
fields
=
None
if
fields
is
not
None
:
if
fields
is
not
None
:
# Ensure that all declared fields have also been included in the
# Ensure that all declared fields have also been included in the
# `Meta.fields` option.
# `Meta.fields` option.
...
...
tests/test_model_serializer.py
View file @
654e0e45
...
@@ -321,6 +321,21 @@ class TestRegularFieldMappings(TestCase):
...
@@ -321,6 +321,21 @@ class TestRegularFieldMappings(TestCase):
ExampleSerializer
()
ExampleSerializer
()
def
test_fields_and_exclude_behavior
(
self
):
class
ImplicitFieldsSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
RegularFieldsModel
class
ExplicitFieldsSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
RegularFieldsModel
fields
=
'__all__'
implicit
=
ImplicitFieldsSerializer
()
explicit
=
ExplicitFieldsSerializer
()
assert
implicit
.
data
==
explicit
.
data
@pytest.mark.skipif
(
django
.
VERSION
<
(
1
,
8
),
@pytest.mark.skipif
(
django
.
VERSION
<
(
1
,
8
),
reason
=
'DurationField is only available for django1.8+'
)
reason
=
'DurationField is only available for django1.8+'
)
...
...
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