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
fe8d95f9
Commit
fe8d95f9
authored
Feb 19, 2015
by
Aider Ibragimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Skip validation of NULL field only if it part of unique_together
parent
3d85473e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
6 deletions
+53
-6
rest_framework/validators.py
+4
-1
tests/test_validators.py
+49
-5
No files found.
rest_framework/validators.py
View file @
fe8d95f9
...
...
@@ -140,7 +140,10 @@ class UniqueTogetherValidator:
queryset
=
self
.
exclude_current_instance
(
attrs
,
queryset
)
# Ignore validation if any field is None
if
None
not
in
attrs
.
values
()
and
queryset
.
exists
():
checked_values
=
[
value
for
field
,
value
in
attrs
.
items
()
if
field
in
self
.
fields
]
if
None
not
in
checked_values
and
queryset
.
exists
():
field_names
=
', '
.
join
(
self
.
fields
)
raise
ValidationError
(
self
.
message
.
format
(
field_names
=
field_names
))
...
...
tests/test_validators.py
View file @
fe8d95f9
...
...
@@ -83,11 +83,37 @@ class UniquenessTogetherModel(models.Model):
unique_together
=
(
'race_name'
,
'position'
)
class
NullUniquenessTogetherModel
(
models
.
Model
):
"""
Used to ensure that null values are not included when checking
unique_together constraints.
Ignoring items which have a null in any of the validated fields is the same
behavior that database backends will use when they have the
unique_together constraint added.
Example case: a null position could indicate a non-finisher in the race,
there could be many non-finishers in a race, but all non-NULL
values *should* be unique against the given `race_name`.
"""
date_of_birth
=
models
.
DateField
(
null
=
True
)
# Not part of the uniqueness constraint
race_name
=
models
.
CharField
(
max_length
=
100
)
position
=
models
.
IntegerField
(
null
=
True
)
class
Meta
:
unique_together
=
(
'race_name'
,
'position'
)
class
UniquenessTogetherSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
UniquenessTogetherModel
class
NullUniquenessTogetherSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
NullUniquenessTogetherModel
class
TestUniquenessTogetherValidation
(
TestCase
):
def
setUp
(
self
):
self
.
instance
=
UniquenessTogetherModel
.
objects
.
create
(
...
...
@@ -183,15 +209,33 @@ class TestUniquenessTogetherValidation(TestCase):
assert
repr
(
serializer
)
==
expected
def
test_ignore_validation_for_null_fields
(
self
):
UniquenessTogetherModel
.
objects
.
create
(
race_name
=
None
,
# None values that are on fields which are part of the uniqueness
# constraint cause the instance to ignore uniqueness validation.
NullUniquenessTogetherModel
.
objects
.
create
(
date_of_birth
=
datetime
.
date
(
2000
,
1
,
1
),
race_name
=
'Paris Marathon'
,
position
=
None
)
data
=
{
'race_name'
:
None
,
'position'
:
None
}
serializer
=
UniquenessTogetherSerializer
(
data
=
data
)
data
=
{
'date'
:
datetime
.
date
(
2000
,
1
,
1
),
'race_name'
:
'Paris Marathon'
,
'position'
:
None
}
serializer
=
NullUniquenessTogetherSerializer
(
data
=
data
)
assert
serializer
.
is_valid
()
def
test_do_not_ignore_validation_for_null_fields
(
self
):
# None values that are not on fields part of the uniqueness constraint
# do not cause the instance to skip validation.
NullUniquenessTogetherModel
.
objects
.
create
(
date_of_birth
=
datetime
.
date
(
2000
,
1
,
1
),
race_name
=
'Paris Marathon'
,
position
=
1
)
data
=
{
'date'
:
None
,
'race_name'
:
'Paris Marathon'
,
'position'
:
1
}
serializer
=
NullUniquenessTogetherSerializer
(
data
=
data
)
assert
not
serializer
.
is_valid
()
# Tests for `UniqueForDateValidator`
# ----------------------------------
...
...
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