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):
})
_related_class
=
PrimaryKeyRelatedField
# Default `create` and `update` behavior...
def
create
(
self
,
validated_data
):
"""
We have a bit of extra checking around this in order to provide
...
...
@@ -791,6 +793,8 @@ class ModelSerializer(Serializer):
return
instance
# Determine the validators to apply...
def
get_validators
(
self
):
"""
Determine the set of validators to use when instantiating serializer.
...
...
@@ -878,28 +882,26 @@ class ModelSerializer(Serializer):
return
validators
# Determine the fields to apply...
def
get_fields
(
self
):
declared_fields
=
copy
.
deepcopy
(
self
.
_declared_fields
)
ret
=
OrderedDict
()
model
=
getattr
(
self
.
Meta
,
'model'
)
depth
=
getattr
(
self
.
Meta
,
'depth'
,
0
)
# Retrieve metadata about fields & relationships on the model class.
info
=
model_meta
.
get_field_info
(
model
)
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
)
uniqueness_extra_kwargs
,
hidden_fields
=
self
.
get_uniqueness_field_options
(
field_names
,
model_fields
)
for
key
,
value
in
uniqueness_extra_kwargs
.
items
():
if
key
in
extra_kwargs
:
extra_kwargs
[
key
]
.
update
(
value
)
else
:
extra_kwargs
[
key
]
=
value
# Determine any extra field arguments and hidden fields that
# should be included
extra_kwargs
=
self
.
get_extra_kwargs
()
extra_kwargs
,
hidden_fields
=
self
.
get_uniqueness_extra_kwargs
(
field_names
,
declared_fields
,
extra_kwargs
)
# Now determine the fields that should be included on the serializer.
ret
=
OrderedDict
()
for
field_name
in
field_names
:
if
field_name
in
declared_fields
:
# Field is explicitly declared on the class, use that.
...
...
@@ -971,15 +973,17 @@ class ModelSerializer(Serializer):
# Create the serializer field.
ret
[
field_name
]
=
field_cls
(
**
kwargs
)
for
field_name
,
field
in
hidden_fields
.
items
():
ret
[
field_name
]
=
field
ret
.
update
(
hidden_fields
)
return
ret
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.
# Returned as a dict of 'model field name' -> 'model field'
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.
Returned as a dict of 'model field name' -> 'model field'.
Used internally by `get_uniqueness_field_options`.
"""
model
=
getattr
(
self
.
Meta
,
'model'
)
model_fields
=
{}
...
...
@@ -1006,8 +1010,18 @@ class ModelSerializer(Serializer):
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_fields
=
self
.
_get_model_fields
(
field_names
,
declared_fields
,
extra_kwargs
)
# Determine if we need any additional `HiddenField` or extra keyword
# arguments to deal with `unique_for` dates that are required to
...
...
@@ -1035,7 +1049,7 @@ class ModelSerializer(Serializer):
# applied, we can add the extra 'required=...' or 'default=...'
# arguments that are appropriate to these fields, or add a `HiddenField` for it.
hidden_fields
=
{}
extra_kwargs
=
{}
uniqueness_
extra_kwargs
=
{}
for
unique_constraint_name
in
unique_constraint_names
:
# Get the model field that is referred too.
...
...
@@ -1053,15 +1067,22 @@ class ModelSerializer(Serializer):
if
unique_constraint_name
in
model_fields
:
# The corresponding field is present in the serializer
if
default
is
empty
:
extra_kwargs
[
unique_constraint_name
]
=
{
'required'
:
True
}
uniqueness_
extra_kwargs
[
unique_constraint_name
]
=
{
'required'
:
True
}
else
:
extra_kwargs
[
unique_constraint_name
]
=
{
'default'
:
default
}
uniqueness_
extra_kwargs
[
unique_constraint_name
]
=
{
'default'
:
default
}
elif
default
is
not
empty
:
# The corresponding field is not present in the,
# serializer. We have a default to use for it, so
# add in a hidden field that populates it.
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
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