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
211bb89e
Commit
211bb89e
authored
Jan 18, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise Validation Errors when relationships receive incorrect types. Fixes #590.
parent
6385ac51
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
9 deletions
+25
-9
rest_framework/relations.py
+11
-9
rest_framework/tests/relations_hyperlink.py
+7
-0
rest_framework/tests/relations_pk.py
+7
-0
rest_framework/tests/relations_slug.py
+0
-0
No files found.
rest_framework/relations.py
View file @
211bb89e
...
...
@@ -177,7 +177,7 @@ class PrimaryKeyRelatedField(RelatedField):
default_error_messages
=
{
'does_not_exist'
:
_
(
"Invalid pk '
%
s' - object does not exist."
),
'in
valid'
:
_
(
'Invalid value
.'
),
'in
correct_type'
:
_
(
'Incorrect type. Expected pk value, received
%
s
.'
),
}
# TODO: Remove these field hacks...
...
...
@@ -208,7 +208,8 @@ class PrimaryKeyRelatedField(RelatedField):
msg
=
self
.
error_messages
[
'does_not_exist'
]
%
smart_unicode
(
data
)
raise
ValidationError
(
msg
)
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'invalid'
]
received
=
type
(
data
)
.
__name__
msg
=
self
.
error_messages
[
'incorrect_type'
]
%
received
raise
ValidationError
(
msg
)
def
field_to_native
(
self
,
obj
,
field_name
):
...
...
@@ -235,7 +236,7 @@ class ManyPrimaryKeyRelatedField(ManyRelatedField):
default_error_messages
=
{
'does_not_exist'
:
_
(
"Invalid pk '
%
s' - object does not exist."
),
'in
valid'
:
_
(
'Invalid value
.'
),
'in
correct_type'
:
_
(
'Incorrect type. Expected pk value, received
%
s
.'
),
}
def
prepare_value
(
self
,
obj
):
...
...
@@ -275,7 +276,8 @@ class ManyPrimaryKeyRelatedField(ManyRelatedField):
msg
=
self
.
error_messages
[
'does_not_exist'
]
%
smart_unicode
(
data
)
raise
ValidationError
(
msg
)
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'invalid'
]
received
=
type
(
data
)
.
__name__
msg
=
self
.
error_messages
[
'incorrect_type'
]
%
received
raise
ValidationError
(
msg
)
### Slug relationships
...
...
@@ -333,7 +335,7 @@ class HyperlinkedRelatedField(RelatedField):
'incorrect_match'
:
_
(
'Invalid hyperlink - Incorrect URL match'
),
'configuration_error'
:
_
(
'Invalid hyperlink due to configuration error'
),
'does_not_exist'
:
_
(
"Invalid hyperlink - object does not exist."
),
'in
valid'
:
_
(
'Invalid value
.'
),
'in
correct_type'
:
_
(
'Incorrect type. Expected url string, received
%
s
.'
),
}
def
__init__
(
self
,
*
args
,
**
kwargs
):
...
...
@@ -397,8 +399,8 @@ class HyperlinkedRelatedField(RelatedField):
try
:
http_prefix
=
value
.
startswith
(
'http:'
)
or
value
.
startswith
(
'https:'
)
except
AttributeError
:
msg
=
self
.
error_messages
[
'in
valid
'
]
raise
ValidationError
(
msg
)
msg
=
self
.
error_messages
[
'in
correct_type
'
]
raise
ValidationError
(
msg
%
type
(
value
)
.
__name__
)
if
http_prefix
:
# If needed convert absolute URLs to relative path
...
...
@@ -434,8 +436,8 @@ class HyperlinkedRelatedField(RelatedField):
except
ObjectDoesNotExist
:
raise
ValidationError
(
self
.
error_messages
[
'does_not_exist'
])
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'in
valid
'
]
raise
ValidationError
(
msg
)
msg
=
self
.
error_messages
[
'in
correct_type
'
]
raise
ValidationError
(
msg
%
type
(
value
)
.
__name__
)
return
obj
...
...
rest_framework/tests/relations_hyperlink.py
View file @
211bb89e
...
...
@@ -215,6 +215,13 @@ class HyperlinkedForeignKeyTests(TestCase):
]
self
.
assertEquals
(
serializer
.
data
,
expected
)
def
test_foreign_key_update_incorrect_type
(
self
):
data
=
{
'url'
:
'/foreignkeysource/1/'
,
'name'
:
u'source-1'
,
'target'
:
2
}
instance
=
ForeignKeySource
.
objects
.
get
(
pk
=
1
)
serializer
=
ForeignKeySourceSerializer
(
instance
,
data
=
data
)
self
.
assertFalse
(
serializer
.
is_valid
())
self
.
assertEquals
(
serializer
.
errors
,
{
'target'
:
[
u'Incorrect type. Expected url string, received int.'
]})
def
test_reverse_foreign_key_update
(
self
):
data
=
{
'url'
:
'/foreignkeytarget/2/'
,
'name'
:
u'target-2'
,
'sources'
:
[
'/foreignkeysource/1/'
,
'/foreignkeysource/3/'
]}
instance
=
ForeignKeyTarget
.
objects
.
get
(
pk
=
2
)
...
...
rest_framework/tests/relations_pk.py
View file @
211bb89e
...
...
@@ -194,6 +194,13 @@ class PKForeignKeyTests(TestCase):
]
self
.
assertEquals
(
serializer
.
data
,
expected
)
def
test_foreign_key_update_incorrect_type
(
self
):
data
=
{
'id'
:
1
,
'name'
:
u'source-1'
,
'target'
:
'foo'
}
instance
=
ForeignKeySource
.
objects
.
get
(
pk
=
1
)
serializer
=
ForeignKeySourceSerializer
(
instance
,
data
=
data
)
self
.
assertFalse
(
serializer
.
is_valid
())
self
.
assertEquals
(
serializer
.
errors
,
{
'target'
:
[
u'Incorrect type. Expected pk value, received str.'
]})
def
test_reverse_foreign_key_update
(
self
):
data
=
{
'id'
:
2
,
'name'
:
u'target-2'
,
'sources'
:
[
1
,
3
]}
instance
=
ForeignKeyTarget
.
objects
.
get
(
pk
=
2
)
...
...
rest_framework/tests/relations_slug.py
View file @
211bb89e
This diff is collapsed.
Click to expand it.
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