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
d92b24a0
Commit
d92b24a0
authored
Nov 01, 2016
by
Ryan P Kilby
Committed by
Tom Christie
Nov 01, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make serializer fields import explicit (#4628)
parent
5c54b227
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
4 deletions
+70
-4
rest_framework/serializers.py
+26
-3
tests/test_serializer.py
+44
-1
No files found.
rest_framework/serializers.py
View file @
d92b24a0
...
@@ -12,18 +12,27 @@ response content is handled by parsers and renderers.
...
@@ -12,18 +12,27 @@ response content is handled by parsers and renderers.
"""
"""
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
copy
import
inspect
import
traceback
import
traceback
from
collections
import
OrderedDict
from
django.core.exceptions
import
ValidationError
as
DjangoValidationError
from
django.core.exceptions
import
ImproperlyConfigured
from
django.db
import
models
from
django.db
import
models
from
django.db.models
import
DurationField
as
ModelDurationField
from
django.db.models
import
DurationField
as
ModelDurationField
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
from
django.utils
import
six
,
timezone
from
django.utils.functional
import
cached_property
from
django.utils.functional
import
cached_property
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework.compat
import
JSONField
as
ModelJSONField
from
rest_framework.compat
import
JSONField
as
ModelJSONField
from
rest_framework.compat
import
postgres_fields
,
set_many
,
unicode_to_repr
from
rest_framework.compat
import
postgres_fields
,
set_many
,
unicode_to_repr
from
rest_framework.utils
import
model_meta
from
rest_framework.exceptions
import
ErrorDetail
,
ValidationError
from
rest_framework.fields
import
get_error_detail
,
set_value
from
rest_framework.settings
import
api_settings
from
rest_framework.utils
import
html
,
model_meta
,
representation
from
rest_framework.utils.field_mapping
import
(
from
rest_framework.utils.field_mapping
import
(
ClassLookupDict
,
get_field_kwargs
,
get_nested_relation_kwargs
,
ClassLookupDict
,
get_field_kwargs
,
get_nested_relation_kwargs
,
get_relation_kwargs
,
get_url_kwargs
get_relation_kwargs
,
get_url_kwargs
...
@@ -42,9 +51,23 @@ from rest_framework.validators import (
...
@@ -42,9 +51,23 @@ from rest_framework.validators import (
#
#
# This helps keep the separation between model fields, form fields, and
# This helps keep the separation between model fields, form fields, and
# serializer fields more explicit.
# serializer fields more explicit.
from
rest_framework.fields
import
(
# NOQA # isort:skip
BooleanField
,
CharField
,
ChoiceField
,
DateField
,
DateTimeField
,
DecimalField
,
DictField
,
DurationField
,
EmailField
,
Field
,
FileField
,
FilePathField
,
FloatField
,
HiddenField
,
IPAddressField
,
ImageField
,
IntegerField
,
JSONField
,
ListField
,
ModelField
,
MultipleChoiceField
,
NullBooleanField
,
ReadOnlyField
,
RegexField
,
SerializerMethodField
,
SlugField
,
TimeField
,
URLField
,
UUIDField
,
)
from
rest_framework.relations
import
(
# NOQA # isort:skip
HyperlinkedIdentityField
,
HyperlinkedRelatedField
,
ManyRelatedField
,
PrimaryKeyRelatedField
,
RelatedField
,
SlugRelatedField
,
StringRelatedField
,
)
from
rest_framework.fields
import
*
# NOQA # isort:skip
# Non-field imports, but public API
from
rest_framework.relations
import
*
# NOQA # isort:skip
from
rest_framework.fields
import
(
# NOQA # isort:skip
CreateOnlyDefault
,
CurrentUserDefault
,
SkipField
,
empty
)
from
rest_framework.relations
import
Hyperlink
,
PKOnlyObject
# NOQA # isort:skip
# We assume that 'validators' are intended for the child serializer,
# We assume that 'validators' are intended for the child serializer,
# rather than the parent serializer.
# rather than the parent serializer.
...
...
tests/test_serializer.py
View file @
d92b24a0
# coding: utf-8
# coding: utf-8
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
inspect
import
pickle
import
pickle
import
re
import
re
import
pytest
import
pytest
from
rest_framework
import
serializers
from
rest_framework
import
fields
,
relations
,
serializers
from
rest_framework.compat
import
unicode_repr
from
rest_framework.compat
import
unicode_repr
from
rest_framework.fields
import
Field
from
.utils
import
MockObject
from
.utils
import
MockObject
# Test serializer fields imports.
# -------------------------------
class
TestFieldImports
:
def
is_field
(
self
,
name
,
value
):
return
(
isinstance
(
value
,
type
)
and
issubclass
(
value
,
Field
)
and
not
name
.
startswith
(
'_'
)
)
def
test_fields
(
self
):
msg
=
"Expected `fields.
%
s` to be imported in `serializers`"
field_classes
=
[
key
for
key
,
value
in
inspect
.
getmembers
(
fields
)
if
self
.
is_field
(
key
,
value
)
]
# sanity check
assert
'Field'
in
field_classes
assert
'BooleanField'
in
field_classes
for
field
in
field_classes
:
assert
hasattr
(
serializers
,
field
),
msg
%
field
def
test_relations
(
self
):
msg
=
"Expected `relations.
%
s` to be imported in `serializers`"
field_classes
=
[
key
for
key
,
value
in
inspect
.
getmembers
(
relations
)
if
self
.
is_field
(
key
,
value
)
]
# sanity check
assert
'RelatedField'
in
field_classes
for
field
in
field_classes
:
assert
hasattr
(
serializers
,
field
),
msg
%
field
# Tests for core functionality.
# Tests for core functionality.
# -----------------------------
# -----------------------------
...
...
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