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
fb1546ee
Commit
fb1546ee
authored
Sep 24, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enforce field_name != source
parent
127c0bd3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
1 deletions
+23
-1
rest_framework/fields.py
+11
-0
tests/test_fields.py
+12
-1
No files found.
rest_framework/fields.py
View file @
fb1546ee
...
...
@@ -160,6 +160,17 @@ class Field(object):
"""
Setup the context for the field instance.
"""
# In order to enforce a consistent style, we error if a redundant
# 'source' argument has been used. For example:
# my_field = serializer.CharField(source='my_field')
assert
self
.
_kwargs
.
get
(
'source'
)
!=
field_name
,
(
"It is redundant to specify `source='
%
s'` on field '
%
s' in "
"serializer '
%
s', as it is the same the field name. "
"Remove the `source` keyword argument."
%
(
field_name
,
self
.
__class__
.
__name__
,
parent
.
__class__
.
__name__
)
)
self
.
field_name
=
field_name
self
.
parent
=
parent
self
.
root
=
root
...
...
tests/test_fields.py
View file @
fb1546ee
from
decimal
import
Decimal
from
django.utils
import
timezone
from
rest_framework
import
fields
from
rest_framework
import
fields
,
serializers
import
datetime
import
django
import
pytest
...
...
@@ -69,6 +69,17 @@ class TestFieldOptions:
output
=
field
.
run_validation
()
assert
output
is
123
def
test_redundant_source
(
self
):
class
ExampleSerializer
(
serializers
.
Serializer
):
example_field
=
serializers
.
CharField
(
source
=
'example_field'
)
with
pytest
.
raises
(
AssertionError
)
as
exc_info
:
ExampleSerializer
()
assert
str
(
exc_info
.
value
)
==
(
"It is redundant to specify `source='example_field'` on field "
"'CharField' in serializer 'ExampleSerializer', as it is the "
"same the field name. Remove the `source` keyword argument."
)
# Tests for field input and output values.
# ----------------------------------------
...
...
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