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
2cf0fda2
Commit
2cf0fda2
authored
Nov 19, 2012
by
jedavis83@gmail.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache default fields per serializer instance for improved performance
parent
a44a94dd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
15 deletions
+10
-15
rest_framework/serializers.py
+10
-15
No files found.
rest_framework/serializers.py
View file @
2cf0fda2
...
@@ -103,6 +103,7 @@ class BaseSerializer(Field):
...
@@ -103,6 +103,7 @@ class BaseSerializer(Field):
self
.
init_data
=
data
self
.
init_data
=
data
self
.
init_files
=
files
self
.
init_files
=
files
self
.
object
=
instance
self
.
object
=
instance
self
.
default_fields
=
self
.
get_default_fields
()
self
.
_data
=
None
self
.
_data
=
None
self
.
_files
=
None
self
.
_files
=
None
...
@@ -111,18 +112,18 @@ class BaseSerializer(Field):
...
@@ -111,18 +112,18 @@ class BaseSerializer(Field):
#####
#####
# Methods to determine which fields to use when (de)serializing objects.
# Methods to determine which fields to use when (de)serializing objects.
def
default_fields
(
self
,
nested
=
False
):
def
get_default_fields
(
self
):
"""
"""
Return the complete set of default fields for the object, as a dict.
Return the complete set of default fields for the object, as a dict.
"""
"""
return
{}
return
{}
def
get_fields
(
self
,
nested
=
False
):
def
get_fields
(
self
):
"""
"""
Returns the complete set of fields for the object as a dict.
Returns the complete set of fields for the object as a dict.
This will be the set of any explicitly declared fields,
This will be the set of any explicitly declared fields,
plus the set of fields returned by default_fields().
plus the set of fields returned by
get_
default_fields().
"""
"""
ret
=
SortedDict
()
ret
=
SortedDict
()
...
@@ -133,8 +134,7 @@ class BaseSerializer(Field):
...
@@ -133,8 +134,7 @@ class BaseSerializer(Field):
field
.
initialize
(
parent
=
self
,
field_name
=
key
)
field
.
initialize
(
parent
=
self
,
field_name
=
key
)
# Add in the default fields
# Add in the default fields
fields
=
self
.
default_fields
(
nested
)
for
key
,
val
in
self
.
default_fields
.
items
():
for
key
,
val
in
fields
.
items
():
if
key
not
in
ret
:
if
key
not
in
ret
:
ret
[
key
]
=
val
ret
[
key
]
=
val
...
@@ -181,7 +181,7 @@ class BaseSerializer(Field):
...
@@ -181,7 +181,7 @@ class BaseSerializer(Field):
ret
=
self
.
_dict_class
()
ret
=
self
.
_dict_class
()
ret
.
fields
=
{}
ret
.
fields
=
{}
fields
=
self
.
get_fields
(
nested
=
bool
(
self
.
opts
.
depth
)
)
fields
=
self
.
get_fields
()
for
field_name
,
field
in
fields
.
items
():
for
field_name
,
field
in
fields
.
items
():
key
=
self
.
get_field_key
(
field_name
)
key
=
self
.
get_field_key
(
field_name
)
value
=
field
.
field_to_native
(
obj
,
field_name
)
value
=
field
.
field_to_native
(
obj
,
field_name
)
...
@@ -194,7 +194,7 @@ class BaseSerializer(Field):
...
@@ -194,7 +194,7 @@ class BaseSerializer(Field):
Core of deserialization, together with `restore_object`.
Core of deserialization, together with `restore_object`.
Converts a dictionary of data into a dictionary of deserialized fields.
Converts a dictionary of data into a dictionary of deserialized fields.
"""
"""
fields
=
self
.
get_fields
(
nested
=
bool
(
self
.
opts
.
depth
)
)
fields
=
self
.
get_fields
()
reverted_data
=
{}
reverted_data
=
{}
for
field_name
,
field
in
fields
.
items
():
for
field_name
,
field
in
fields
.
items
():
try
:
try
:
...
@@ -209,7 +209,7 @@ class BaseSerializer(Field):
...
@@ -209,7 +209,7 @@ class BaseSerializer(Field):
Run `validate_<fieldname>()` and `validate()` methods on the serializer
Run `validate_<fieldname>()` and `validate()` methods on the serializer
"""
"""
# TODO: refactor this so we're not determining the fields again
# TODO: refactor this so we're not determining the fields again
fields
=
self
.
get_fields
(
nested
=
bool
(
self
.
opts
.
depth
)
)
fields
=
self
.
get_fields
()
for
field_name
,
field
in
fields
.
items
():
for
field_name
,
field
in
fields
.
items
():
try
:
try
:
...
@@ -332,16 +332,10 @@ class ModelSerializer(Serializer):
...
@@ -332,16 +332,10 @@ class ModelSerializer(Serializer):
"""
"""
_options_class
=
ModelSerializerOptions
_options_class
=
ModelSerializerOptions
def
default_fields
(
self
,
nested
=
False
):
def
get_default_fields
(
self
):
"""
"""
Return all the fields that should be serialized for the model.
Return all the fields that should be serialized for the model.
"""
"""
# TODO: Modify this so that it's called on init, and drop
# serialize/obj/data arguments.
#
# We *could* provide a hook for dynamic fields, but
# it'd be nice if the default was to generate fields statically
# at the point of __init__
cls
=
self
.
opts
.
model
cls
=
self
.
opts
.
model
opts
=
get_concrete_model
(
cls
)
.
_meta
opts
=
get_concrete_model
(
cls
)
.
_meta
...
@@ -353,6 +347,7 @@ class ModelSerializer(Serializer):
...
@@ -353,6 +347,7 @@ class ModelSerializer(Serializer):
fields
+=
[
field
for
field
in
opts
.
many_to_many
if
field
.
serialize
]
fields
+=
[
field
for
field
in
opts
.
many_to_many
if
field
.
serialize
]
ret
=
SortedDict
()
ret
=
SortedDict
()
nested
=
bool
(
self
.
opts
.
depth
)
is_pk
=
True
# First field in the list is the pk
is_pk
=
True
# First field in the list is the pk
for
model_field
in
fields
:
for
model_field
in
fields
:
...
...
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