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
4a112fc3
Commit
4a112fc3
authored
Dec 19, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up
parent
caa13181
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
21 deletions
+42
-21
rest_framework/serializers.py
+42
-21
No files found.
rest_framework/serializers.py
View file @
4a112fc3
...
@@ -722,6 +722,8 @@ class ModelSerializer(Serializer):
...
@@ -722,6 +722,8 @@ class ModelSerializer(Serializer):
})
})
_related_class
=
PrimaryKeyRelatedField
_related_class
=
PrimaryKeyRelatedField
# Default `create` and `update` behavior...
def
create
(
self
,
validated_data
):
def
create
(
self
,
validated_data
):
"""
"""
We have a bit of extra checking around this in order to provide
We have a bit of extra checking around this in order to provide
...
@@ -791,6 +793,8 @@ class ModelSerializer(Serializer):
...
@@ -791,6 +793,8 @@ class ModelSerializer(Serializer):
return
instance
return
instance
# Determine the validators to apply...
def
get_validators
(
self
):
def
get_validators
(
self
):
"""
"""
Determine the set of validators to use when instantiating serializer.
Determine the set of validators to use when instantiating serializer.
...
@@ -878,28 +882,26 @@ class ModelSerializer(Serializer):
...
@@ -878,28 +882,26 @@ class ModelSerializer(Serializer):
return
validators
return
validators
# Determine the fields to apply...
def
get_fields
(
self
):
def
get_fields
(
self
):
declared_fields
=
copy
.
deepcopy
(
self
.
_declared_fields
)
declared_fields
=
copy
.
deepcopy
(
self
.
_declared_fields
)
ret
=
OrderedDict
()
model
=
getattr
(
self
.
Meta
,
'model'
)
model
=
getattr
(
self
.
Meta
,
'model'
)
depth
=
getattr
(
self
.
Meta
,
'depth'
,
0
)
depth
=
getattr
(
self
.
Meta
,
'depth'
,
0
)
# Retrieve metadata about fields & relationships on the model class.
# Retrieve metadata about fields & relationships on the model class.
info
=
model_meta
.
get_field_info
(
model
)
info
=
model_meta
.
get_field_info
(
model
)
field_names
=
self
.
get_field_names
(
declared_fields
,
info
)
field_names
=
self
.
get_field_names
(
declared_fields
,
info
)
extra_kwargs
=
self
.
get_extra_kwargs
()
model_fields
=
self
.
get_model_fields
(
field_names
,
declared_fields
,
extra_kwargs
)
# Determine any extra field arguments and hidden fields that
uniqueness_extra_kwargs
,
hidden_fields
=
self
.
get_uniqueness_field_options
(
field_names
,
model_fields
)
# should be included
for
key
,
value
in
uniqueness_extra_kwargs
.
items
():
extra_kwargs
=
self
.
get_extra_kwargs
()
if
key
in
extra_kwargs
:
extra_kwargs
,
hidden_fields
=
self
.
get_uniqueness_extra_kwargs
(
extra_kwargs
[
key
]
.
update
(
value
)
field_names
,
declared_fields
,
extra_kwargs
else
:
)
extra_kwargs
[
key
]
=
value
# Now determine the fields that should be included on the serializer.
# Now determine the fields that should be included on the serializer.
ret
=
OrderedDict
()
for
field_name
in
field_names
:
for
field_name
in
field_names
:
if
field_name
in
declared_fields
:
if
field_name
in
declared_fields
:
# Field is explicitly declared on the class, use that.
# Field is explicitly declared on the class, use that.
...
@@ -971,15 +973,17 @@ class ModelSerializer(Serializer):
...
@@ -971,15 +973,17 @@ class ModelSerializer(Serializer):
# Create the serializer field.
# Create the serializer field.
ret
[
field_name
]
=
field_cls
(
**
kwargs
)
ret
[
field_name
]
=
field_cls
(
**
kwargs
)
for
field_name
,
field
in
hidden_fields
.
items
():
ret
.
update
(
hidden_fields
)
ret
[
field_name
]
=
field
return
ret
return
ret
def
get_model_fields
(
self
,
field_names
,
declared_fields
,
extra_kwargs
):
def
_get_model_fields
(
self
,
field_names
,
declared_fields
,
extra_kwargs
):
# Returns all the model fields that are being mapped to by fields
"""
# on the serializer class.
Returns all the model fields that are being mapped to by fields
# Returned as a dict of 'model field name' -> 'model field'
on the serializer class.
Returned as a dict of 'model field name' -> 'model field'.
Used internally by `get_uniqueness_field_options`.
"""
model
=
getattr
(
self
.
Meta
,
'model'
)
model
=
getattr
(
self
.
Meta
,
'model'
)
model_fields
=
{}
model_fields
=
{}
...
@@ -1006,8 +1010,18 @@ class ModelSerializer(Serializer):
...
@@ -1006,8 +1010,18 @@ class ModelSerializer(Serializer):
return
model_fields
return
model_fields
def
get_uniqueness_field_options
(
self
,
field_names
,
model_fields
):
def
get_uniqueness_extra_kwargs
(
self
,
field_names
,
declared_fields
,
extra_kwargs
):
"""
Return any additional field options that need to be included as a
result of uniqueness constraints on the model. This is returned as
a two-tuple of:
('dict of updated extra kwargs', 'mapping of hidden fields')
"""
model
=
getattr
(
self
.
Meta
,
'model'
)
model
=
getattr
(
self
.
Meta
,
'model'
)
model_fields
=
self
.
_get_model_fields
(
field_names
,
declared_fields
,
extra_kwargs
)
# Determine if we need any additional `HiddenField` or extra keyword
# Determine if we need any additional `HiddenField` or extra keyword
# arguments to deal with `unique_for` dates that are required to
# arguments to deal with `unique_for` dates that are required to
...
@@ -1035,7 +1049,7 @@ class ModelSerializer(Serializer):
...
@@ -1035,7 +1049,7 @@ class ModelSerializer(Serializer):
# applied, we can add the extra 'required=...' or 'default=...'
# applied, we can add the extra 'required=...' or 'default=...'
# arguments that are appropriate to these fields, or add a `HiddenField` for it.
# arguments that are appropriate to these fields, or add a `HiddenField` for it.
hidden_fields
=
{}
hidden_fields
=
{}
extra_kwargs
=
{}
uniqueness_
extra_kwargs
=
{}
for
unique_constraint_name
in
unique_constraint_names
:
for
unique_constraint_name
in
unique_constraint_names
:
# Get the model field that is referred too.
# Get the model field that is referred too.
...
@@ -1053,15 +1067,22 @@ class ModelSerializer(Serializer):
...
@@ -1053,15 +1067,22 @@ class ModelSerializer(Serializer):
if
unique_constraint_name
in
model_fields
:
if
unique_constraint_name
in
model_fields
:
# The corresponding field is present in the serializer
# The corresponding field is present in the serializer
if
default
is
empty
:
if
default
is
empty
:
extra_kwargs
[
unique_constraint_name
]
=
{
'required'
:
True
}
uniqueness_
extra_kwargs
[
unique_constraint_name
]
=
{
'required'
:
True
}
else
:
else
:
extra_kwargs
[
unique_constraint_name
]
=
{
'default'
:
default
}
uniqueness_
extra_kwargs
[
unique_constraint_name
]
=
{
'default'
:
default
}
elif
default
is
not
empty
:
elif
default
is
not
empty
:
# The corresponding field is not present in the,
# The corresponding field is not present in the,
# serializer. We have a default to use for it, so
# serializer. We have a default to use for it, so
# add in a hidden field that populates it.
# add in a hidden field that populates it.
hidden_fields
[
unique_constraint_name
]
=
HiddenField
(
default
=
default
)
hidden_fields
[
unique_constraint_name
]
=
HiddenField
(
default
=
default
)
# Update `extra_kwargs` with any new options.
for
key
,
value
in
uniqueness_extra_kwargs
.
items
():
if
key
in
extra_kwargs
:
extra_kwargs
[
key
]
.
update
(
value
)
else
:
extra_kwargs
[
key
]
=
value
return
extra_kwargs
,
hidden_fields
return
extra_kwargs
,
hidden_fields
def
get_extra_kwargs
(
self
):
def
get_extra_kwargs
(
self
):
...
...
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