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
a97c9892
Commit
a97c9892
authored
May 28, 2015
by
Jannon Frank
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix read_only related field metadata
parent
d4dd22ff
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
1 deletions
+84
-1
rest_framework/metadata.py
+1
-1
tests/test_metadata.py
+83
-0
No files found.
rest_framework/metadata.py
View file @
a97c9892
...
@@ -127,7 +127,7 @@ class SimpleMetadata(BaseMetadata):
...
@@ -127,7 +127,7 @@ class SimpleMetadata(BaseMetadata):
if
value
is
not
None
and
value
!=
''
:
if
value
is
not
None
and
value
!=
''
:
field_info
[
attr
]
=
force_text
(
value
,
strings_only
=
True
)
field_info
[
attr
]
=
force_text
(
value
,
strings_only
=
True
)
if
hasattr
(
field
,
'choices'
):
if
not
field_info
.
get
(
'read_only'
)
and
hasattr
(
field
,
'choices'
):
field_info
[
'choices'
]
=
[
field_info
[
'choices'
]
=
[
{
{
'value'
:
choice_value
,
'value'
:
choice_value
,
...
...
tests/test_metadata.py
View file @
a97c9892
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
django.db
import
models
from
django.test
import
TestCase
from
django.core.validators
import
MinValueValidator
,
MaxValueValidator
from
rest_framework
import
exceptions
,
metadata
,
serializers
,
status
,
views
,
versioning
from
rest_framework
import
exceptions
,
metadata
,
serializers
,
status
,
views
,
versioning
from
rest_framework.request
import
Request
from
rest_framework.request
import
Request
from
rest_framework.renderers
import
BrowsableAPIRenderer
from
rest_framework.renderers
import
BrowsableAPIRenderer
...
@@ -212,3 +215,83 @@ class TestMetadata:
...
@@ -212,3 +215,83 @@ class TestMetadata:
options
=
metadata
.
SimpleMetadata
()
options
=
metadata
.
SimpleMetadata
()
field_info
=
options
.
get_field_info
(
serializers
.
NullBooleanField
())
field_info
=
options
.
get_field_info
(
serializers
.
NullBooleanField
())
assert
field_info
[
'type'
]
==
'boolean'
assert
field_info
[
'type'
]
==
'boolean'
class
TestModelSerializerMetadata
(
TestCase
):
def
test_read_only_primary_key_related_field
(
self
):
"""
On generic views OPTIONS should return an 'actions' key with metadata
on the fields that may be supplied to PUT and POST requests. It should
not fail when a read_only PrimaryKeyRelatedField is present
"""
class
Parent
(
models
.
Model
):
integer_field
=
models
.
IntegerField
(
validators
=
[
MinValueValidator
(
1
),
MaxValueValidator
(
1000
)])
children
=
models
.
ManyToManyField
(
'Child'
)
name
=
models
.
CharField
(
max_length
=
100
,
blank
=
True
,
null
=
True
)
class
Child
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
100
)
class
ExampleSerializer
(
serializers
.
ModelSerializer
):
children
=
serializers
.
PrimaryKeyRelatedField
(
read_only
=
True
,
many
=
True
)
class
Meta
:
model
=
Parent
class
ExampleView
(
views
.
APIView
):
"""Example view."""
def
post
(
self
,
request
):
pass
def
get_serializer
(
self
):
return
ExampleSerializer
()
view
=
ExampleView
.
as_view
()
response
=
view
(
request
=
request
)
expected
=
{
'name'
:
'Example'
,
'description'
:
'Example view.'
,
'renders'
:
[
'application/json'
,
'text/html'
],
'parses'
:
[
'application/json'
,
'application/x-www-form-urlencoded'
,
'multipart/form-data'
],
'actions'
:
{
'POST'
:
{
'id'
:
{
'type'
:
'integer'
,
'required'
:
False
,
'read_only'
:
True
,
'label'
:
'ID'
},
'children'
:
{
'type'
:
'field'
,
'required'
:
False
,
'read_only'
:
True
,
'label'
:
'Children'
},
'integer_field'
:
{
'type'
:
'integer'
,
'required'
:
True
,
'read_only'
:
False
,
'label'
:
'Integer field'
,
'min_value'
:
1
,
'max_value'
:
1000
},
'name'
:
{
'type'
:
'string'
,
'required'
:
False
,
'read_only'
:
False
,
'label'
:
'Name'
,
'max_length'
:
100
}
}
}
}
assert
response
.
status_code
==
status
.
HTTP_200_OK
assert
response
.
data
==
expected
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