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
ab40780d
Commit
ab40780d
authored
Sep 11, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidy up lookup_class
parent
3318f75a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
10 deletions
+11
-10
rest_framework/serializers.py
+11
-10
No files found.
rest_framework/serializers.py
View file @
ab40780d
...
...
@@ -317,17 +317,17 @@ class ModelSerializerOptions(object):
self
.
depth
=
getattr
(
meta
,
'depth'
,
0
)
def
lookup_class
(
mapping
,
obj
):
def
lookup_class
(
mapping
,
instance
):
"""
Takes a dictionary with classes as keys, and an object.
Traverses the object's inheritance hierarchy in method
resolution order, and returns the first matching value
from the dictionary or
None
.
from the dictionary or
raises a KeyError if nothing matches
.
"""
return
next
(
(
mapping
[
cls
]
for
cls
in
inspect
.
getmro
(
obj
.
__class__
)
if
cls
in
mapping
),
None
)
for
cls
in
inspect
.
getmro
(
instance
.
__class__
):
if
cls
in
mapping
:
return
mapping
[
cls
]
raise
KeyError
(
'Class
%
s not found in lookup.'
,
cls
.
__name__
)
class
ModelSerializer
(
Serializer
):
...
...
@@ -341,6 +341,7 @@ class ModelSerializer(Serializer):
models
.
DateTimeField
:
DateTimeField
,
models
.
DecimalField
:
DecimalField
,
models
.
EmailField
:
EmailField
,
models
.
Field
:
ModelField
,
models
.
FileField
:
FileField
,
models
.
FloatField
:
FloatField
,
models
.
ImageField
:
ImageField
,
...
...
@@ -484,6 +485,7 @@ class ModelSerializer(Serializer):
"""
Creates a default instance of a basic non-relational field.
"""
serializer_cls
=
lookup_class
(
self
.
field_mapping
,
model_field
)
kwargs
=
{}
validator_kwarg
=
model_field
.
validators
...
...
@@ -602,11 +604,10 @@ class ModelSerializer(Serializer):
if
validator_kwarg
:
kwargs
[
'validators'
]
=
validator_kwarg
cls
=
lookup_class
(
self
.
field_mapping
,
model_field
)
if
cls
is
None
:
cls
=
ModelField
if
issubclass
(
serializer_cls
,
ModelField
):
kwargs
[
'model_field'
]
=
model_field
return
cls
(
**
kwargs
)
return
serializer_cls
(
**
kwargs
)
class
HyperlinkedModelSerializerOptions
(
ModelSerializerOptions
):
...
...
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