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
bc87bf13
Commit
bc87bf13
authored
Feb 22, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests and fix for #666.
Closes #666.
parent
d44eb209
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
6 deletions
+64
-6
rest_framework/mixins.py
+27
-3
rest_framework/serializers.py
+4
-2
rest_framework/tests/generics.py
+33
-1
No files found.
rest_framework/mixins.py
View file @
bc87bf13
...
...
@@ -12,6 +12,28 @@ from rest_framework.response import Response
from
rest_framework.request
import
clone_request
def
_get_validation_exclusions
(
obj
,
pk
=
None
,
slug_field
=
None
):
"""
Given a model instance, and an optional pk and slug field,
return the full list of all other field names on that model.
For use when performing full_clean on a model instance,
so we only clean the required fields.
"""
include
=
[]
if
pk
:
pk_field
=
obj
.
_meta
.
pk
while
pk_field
.
rel
:
pk_field
=
pk_field
.
rel
.
to
.
_meta
.
pk
include
.
append
(
pk_field
.
name
)
if
slug_field
:
include
.
append
(
slug_field
)
return
[
field
.
name
for
field
in
obj
.
_meta
.
fields
if
field
.
name
not
in
include
]
class
CreateModelMixin
(
object
):
"""
Create a model instance.
...
...
@@ -117,18 +139,20 @@ class UpdateModelMixin(object):
"""
# pk and/or slug attributes are implicit in the URL.
pk
=
self
.
kwargs
.
get
(
self
.
pk_url_kwarg
,
None
)
slug
=
self
.
kwargs
.
get
(
self
.
slug_url_kwarg
,
None
)
slug_field
=
slug
and
self
.
get_slug_field
()
or
None
if
pk
:
setattr
(
obj
,
'pk'
,
pk
)
slug
=
self
.
kwargs
.
get
(
self
.
slug_url_kwarg
,
None
)
if
slug
:
slug_field
=
self
.
get_slug_field
()
setattr
(
obj
,
slug_field
,
slug
)
# Ensure we clean the attributes so that we don't eg return integer
# pk using a string representation, as provided by the url conf kwarg.
if
hasattr
(
obj
,
'full_clean'
):
obj
.
full_clean
()
exclude
=
_get_validation_exclusions
(
obj
,
pk
,
slug_field
)
obj
.
full_clean
(
exclude
)
class
DestroyModelMixin
(
object
):
...
...
rest_framework/serializers.py
View file @
bc87bf13
...
...
@@ -158,6 +158,7 @@ class BaseSerializer(Field):
# If 'fields' is specified, use those fields, in that order.
if
self
.
opts
.
fields
:
assert
isinstance
(
self
.
opts
.
fields
,
(
list
,
tuple
)),
'`include` must be a list or tuple'
new
=
SortedDict
()
for
key
in
self
.
opts
.
fields
:
new
[
key
]
=
ret
[
key
]
...
...
@@ -165,6 +166,7 @@ class BaseSerializer(Field):
# Remove anything in 'exclude'
if
self
.
opts
.
exclude
:
assert
isinstance
(
self
.
opts
.
fields
,
(
list
,
tuple
)),
'`exclude` must be a list or tuple'
for
key
in
self
.
opts
.
exclude
:
ret
.
pop
(
key
,
None
)
...
...
@@ -421,8 +423,8 @@ class ModelSerializer(Serializer):
cls
=
self
.
opts
.
model
opts
=
get_concrete_model
(
cls
)
.
_meta
pk_field
=
opts
.
pk
while
pk_field
.
rel
:
pk_field
=
pk_field
.
rel
.
to
.
_meta
.
pk
#
while pk_field.rel:
#
pk_field = pk_field.rel.to._meta.pk
fields
=
[
pk_field
]
fields
+=
[
field
for
field
in
opts
.
fields
if
field
.
serialize
]
fields
+=
[
field
for
field
in
opts
.
many_to_many
if
field
.
serialize
]
...
...
rest_framework/tests/generics.py
View file @
bc87bf13
...
...
@@ -319,7 +319,7 @@ class TestCreateModelWithAutoNowAddField(TestCase):
self
.
assertEquals
(
created
.
content
,
'foobar'
)
# Test for particularly ugly reression with m2m in browseable API
# Test for particularly ugly re
g
ression with m2m in browseable API
class
ClassB
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
255
)
...
...
@@ -350,3 +350,35 @@ class TestM2MBrowseableAPI(TestCase):
view
=
ExampleView
()
.
as_view
()
response
=
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
# Regression for #666
class
ValidationModel
(
models
.
Model
):
blank_validated_field
=
models
.
CharField
(
max_length
=
255
)
class
ValidationModelSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
ValidationModel
fields
=
(
'blank_validated_field'
,)
read_only_fields
=
(
'blank_validated_field'
,)
class
UpdateValidationModel
(
generics
.
RetrieveUpdateDestroyAPIView
):
model
=
ValidationModel
serializer_class
=
ValidationModelSerializer
class
TestPreSaveValidationExclusions
(
TestCase
):
def
test_pre_save_validation_exclusions
(
self
):
"""
Somewhat weird test case to ensure that we don't perform model
validation on read only fields.
"""
obj
=
ValidationModel
.
objects
.
create
(
blank_validated_field
=
''
)
request
=
factory
.
put
(
'/'
,
json
.
dumps
({}),
content_type
=
'application/json'
)
view
=
UpdateValidationModel
()
.
as_view
()
response
=
view
(
request
,
pk
=
obj
.
pk
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
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