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
0576241b
Commit
0576241b
authored
Dec 23, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #523 from maspwr/related-required
RelatedField should respect self.required
parents
ab991199
f8a1256b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletions
+38
-1
rest_framework/fields.py
+6
-1
rest_framework/tests/serializer.py
+32
-0
No files found.
rest_framework/fields.py
View file @
0576241b
...
...
@@ -351,7 +351,12 @@ class RelatedField(WritableField):
if
self
.
read_only
:
return
value
=
data
.
get
(
field_name
)
try
:
value
=
data
[
field_name
]
except
KeyError
:
if
self
.
required
:
raise
ValidationError
(
self
.
error_messages
[
'required'
])
return
if
value
in
(
None
,
''
)
and
not
self
.
null
:
raise
ValidationError
(
'Value may not be null'
)
...
...
rest_framework/tests/serializer.py
View file @
0576241b
...
...
@@ -308,6 +308,38 @@ class ModelValidationTests(TestCase):
self
.
assertFalse
(
second_serializer
.
is_valid
())
self
.
assertEqual
(
second_serializer
.
errors
,
{
'title'
:
[
u'Album with this Title already exists.'
]})
def
test_foreign_key_with_partial
(
self
):
"""
Test ModelSerializer validation with partial=True
Specifically test foreign key validation.
"""
album
=
Album
(
title
=
'test'
)
album
.
save
()
class
PhotoSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Photo
photo_serializer
=
PhotoSerializer
(
data
=
{
'description'
:
'test'
,
'album'
:
album
.
pk
})
self
.
assertTrue
(
photo_serializer
.
is_valid
())
photo
=
photo_serializer
.
save
()
# Updating only the album (foreign key)
photo_serializer
=
PhotoSerializer
(
instance
=
photo
,
data
=
{
'album'
:
album
.
pk
},
partial
=
True
)
self
.
assertTrue
(
photo_serializer
.
is_valid
())
self
.
assertTrue
(
photo_serializer
.
save
())
# Updating only the description
photo_serializer
=
PhotoSerializer
(
instance
=
photo
,
data
=
{
'description'
:
'new'
},
partial
=
True
)
self
.
assertTrue
(
photo_serializer
.
is_valid
())
self
.
assertTrue
(
photo_serializer
.
save
())
class
RegexValidationTest
(
TestCase
):
def
test_create_failed
(
self
):
...
...
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