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
5830f7e1
Commit
5830f7e1
authored
Dec 19, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get_unique_together_validators and get_unique_for_date_validators
parent
4ea14029
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
9 deletions
+42
-9
rest_framework/serializers.py
+42
-9
No files found.
rest_framework/serializers.py
View file @
5830f7e1
...
...
@@ -792,14 +792,33 @@ class ModelSerializer(Serializer):
return
instance
def
get_validators
(
self
):
"""
Determine the set of validators to use when instantiating serializer.
"""
# If the validators have been declared explicitly then use that.
validators
=
getattr
(
getattr
(
self
,
'Meta'
,
None
),
'validators'
,
None
)
if
validators
is
not
None
:
return
validators
# Determine the default set of validators.
validators
=
[]
model_class
=
self
.
Meta
.
model
# Otherwise use the default set of validators.
return
(
self
.
get_unique_together_validators
()
+
self
.
get_unique_for_date_validators
()
)
def
get_unique_together_validators
(
self
):
"""
Determine a default set of validators for any unique_together contraints.
"""
model_class_inheritance_tree
=
(
[
self
.
Meta
.
model
]
+
list
(
self
.
Meta
.
model
.
_meta
.
parents
.
keys
())
)
# The field names we're passing though here only include fields
# which may map onto a model field. Any dotted field name lookups
# cannot map to a field, and must be a traversal, so we're not
# including those.
field_names
=
set
([
field
.
source
for
field
in
self
.
fields
.
values
()
if
(
field
.
source
!=
'*'
)
and
(
'.'
not
in
field
.
source
)
...
...
@@ -807,7 +826,8 @@ class ModelSerializer(Serializer):
# Note that we make sure to check `unique_together` both on the
# base model class, but also on any parent classes.
for
parent_class
in
[
model_class
]
+
list
(
model_class
.
_meta
.
parents
.
keys
()):
validators
=
[]
for
parent_class
in
model_class_inheritance_tree
:
for
unique_together
in
parent_class
.
_meta
.
unique_together
:
if
field_names
.
issuperset
(
set
(
unique_together
)):
validator
=
UniqueTogetherValidator
(
...
...
@@ -815,13 +835,26 @@ class ModelSerializer(Serializer):
fields
=
unique_together
)
validators
.
append
(
validator
)
return
validators
def
get_unique_for_date_validators
(
self
):
"""
Determine a default set of validators for the following contraints:
* unique_for_date
* unique_for_month
* unique_for_year
"""
info
=
model_meta
.
get_field_info
(
self
.
Meta
.
model
)
default_manager
=
self
.
Meta
.
model
.
_default_manager
field_names
=
[
field
.
source
for
field
in
self
.
fields
.
values
()]
validators
=
[]
# Add any unique_for_date/unique_for_month/unique_for_year constraints.
info
=
model_meta
.
get_field_info
(
model_class
)
for
field_name
,
field
in
info
.
fields_and_pk
.
items
():
if
field
.
unique_for_date
and
field_name
in
field_names
:
validator
=
UniqueForDateValidator
(
queryset
=
model_class
.
_
default_manager
,
queryset
=
default_manager
,
field
=
field_name
,
date_field
=
field
.
unique_for_date
)
...
...
@@ -829,7 +862,7 @@ class ModelSerializer(Serializer):
if
field
.
unique_for_month
and
field_name
in
field_names
:
validator
=
UniqueForMonthValidator
(
queryset
=
model_class
.
_
default_manager
,
queryset
=
default_manager
,
field
=
field_name
,
date_field
=
field
.
unique_for_month
)
...
...
@@ -837,7 +870,7 @@ class ModelSerializer(Serializer):
if
field
.
unique_for_year
and
field_name
in
field_names
:
validator
=
UniqueForYearValidator
(
queryset
=
model_class
.
_
default_manager
,
queryset
=
default_manager
,
field
=
field_name
,
date_field
=
field
.
unique_for_year
)
...
...
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