Skip to content

Skip unnecessary unique together checking #9154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions rest_framework/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,19 @@ def __call__(self, attrs, serializer):
queryset = self.exclude_current_instance(attrs, queryset, serializer.instance)

# Ignore validation if any field is None
checked_values = [
value for field, value in attrs.items() if field in self.fields
]
if None not in checked_values and qs_exists(queryset):
if serializer.instance is None:
checked_values = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this change have any effect on schema migrations? thanks for the patch

Copy link
Contributor Author

@yuekui yuekui Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @auvipy I believe there won't be any changes to the schema or any impact on migrations.

value for field, value in attrs.items() if field in self.fields
]
else:
# Ignore validation if all field values are unchanged
checked_values = [
value
for field, value in attrs.items()
if field in self.fields and value != getattr(serializer.instance, field)
]

if checked_values and None not in checked_values and qs_exists(queryset):
field_names = ', '.join(self.fields)
message = self.message.format(field_names=field_names)
raise ValidationError(message, code='unique')
Expand Down
18 changes: 17 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import pytest
from django.db import DataError, models
Expand Down Expand Up @@ -447,6 +447,22 @@ def test_do_not_ignore_validation_for_null_fields(self):
serializer = NullUniquenessTogetherSerializer(data=data)
assert not serializer.is_valid()

def test_ignore_validation_for_unchanged_fields(self):
Copy link
Contributor Author

@yuekui yuekui Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@auvipy This is a new test I added for the scenario where none of the fields in the unique-together constraint are changed. For other cases, such as when any of the field values change, they are already covered by other test cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking for if more additional tests could be provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, though I believe all the cases have already been covered, I might have overlooked something. Could you suggest what types of additional tests might be needed?

"""
If all fields in the unique together constraint are unchanged,
then the instance should skip uniqueness validation.
"""
instance = UniquenessTogetherModel.objects.create(
race_name="Paris Marathon", position=1
)
data = {"race_name": "Paris Marathon", "position": 1}
serializer = UniquenessTogetherSerializer(data=data, instance=instance)
with patch(
"rest_framework.validators.qs_exists"
) as mock:
assert serializer.is_valid()
assert not mock.called

def test_filter_queryset_do_not_skip_existing_attribute(self):
"""
filter_queryset should add value from existing instance attribute
Expand Down