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
adcb64ab
Commit
adcb64ab
authored
Sep 12, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MethodField -> SerializerMethodField
parent
1e53eb0a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
14 additions
and
15 deletions
+14
-15
rest_framework/fields.py
+9
-12
rest_framework/serializers.py
+2
-1
rest_framework/utils/modelinfo.py
+2
-1
tests/test_serializer_empty.py
+1
-1
No files found.
rest_framework/fields.py
View file @
adcb64ab
...
...
@@ -768,16 +768,13 @@ class ReadOnlyField(Field):
kwargs
[
'read_only'
]
=
True
super
(
ReadOnlyField
,
self
)
.
__init__
(
**
kwargs
)
def
to_native
(
self
,
data
):
raise
NotImplemented
(
'.to_native() not supported.'
)
def
to_primative
(
self
,
value
):
if
is_simple_callable
(
value
):
return
value
()
return
value
class
MethodField
(
Field
):
class
Serializer
MethodField
(
Field
):
"""
A read-only field that get its representation from calling a method on the
parent serializer class. The method called will be of the form
...
...
@@ -787,22 +784,22 @@ class MethodField(Field):
For example:
class ExampleSerializer(self):
extra_info = MethodField()
extra_info =
Serializer
MethodField()
def get_extra_info(self, obj):
return ... # Calculate some data to return.
"""
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
method_attr
=
None
,
**
kwargs
):
self
.
method_attr
=
method_attr
kwargs
[
'source'
]
=
'*'
kwargs
[
'read_only'
]
=
True
super
(
MethodField
,
self
)
.
__init__
(
**
kwargs
)
def
to_native
(
self
,
data
):
raise
NotImplemented
(
'.to_native() not supported.'
)
super
(
SerializerMethodField
,
self
)
.
__init__
(
**
kwargs
)
def
to_primative
(
self
,
value
):
attr
=
'get_{field_name}'
.
format
(
field_name
=
self
.
field_name
)
method
=
getattr
(
self
.
parent
,
attr
)
method_attr
=
self
.
method_attr
if
method_attr
is
None
:
method_attr
=
'get_{field_name}'
.
format
(
field_name
=
self
.
field_name
)
method
=
getattr
(
self
.
parent
,
method_attr
)
return
method
(
value
)
...
...
rest_framework/serializers.py
View file @
adcb64ab
...
...
@@ -598,7 +598,8 @@ class ModelSerializer(Serializer):
if
isinstance
(
model_field
,
models
.
BooleanField
):
# models.BooleanField has `blank=True`, but *is* actually
# required *unless* a default is provided.
# Also note that <1.6 `default=False`, >=1.6 `default=None`.
# Also note that Django<1.6 uses `default=False` for
# models.BooleanField, but Django>=1.6 uses `default=None`.
kwargs
.
pop
(
'required'
,
None
)
if
validator_kwarg
:
...
...
rest_framework/utils/modelinfo.py
View file @
adcb64ab
"""
Helper functions for returning the field information that is associated
with a model class.
with a model class. This includes returning all the forward and reverse
relationships and their associated metadata.
"""
from
collections
import
namedtuple
from
django.db
import
models
...
...
tests/test_serializer_empty.py
View file @
adcb64ab
...
...
@@ -6,7 +6,7 @@
# def test_empty_serializer(self):
# class FooBarSerializer(serializers.Serializer):
# foo = serializers.IntegerField()
# bar = serializers.MethodField()
# bar = serializers.
Serializer
MethodField()
# def get_bar(self, obj):
# return 'bar'
...
...
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