-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix validators crashing with AttributeError when many=True passes a queryset as instance #9975
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
Open
LeSingh1
wants to merge
7
commits into
encode:main
Choose a base branch
from
LeSingh1:fix/unique-validators-many-true-queryset-instance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−19
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08be7da
Fix validators crashing with AttributeError when many=True passes a q…
LeSingh1 1e269e3
Fix validators to guard isinstance(instance, Model) to handle ListSer…
LeSingh1 2024288
Add regression test for ListSerializer many=True with unique constrai…
LeSingh1 bfe1ff3
Thread instance through filter_queryset per review feedback
LeSingh1 61443c5
Potential fix for pull request finding
auvipy dc37d8a
Fix pre-commit lint errors in validators and test file
LeSingh1 072fbc0
Merge branch 'main' into fix/unique-validators-many-true-queryset-ins…
auvipy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| """ | ||
| from django.core.exceptions import FieldError | ||
| from django.db import DataError | ||
| from django.db.models import Exists | ||
| from django.db.models import Exists, Model | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| from rest_framework.exceptions import ValidationError | ||
|
|
@@ -69,7 +69,7 @@ def exclude_current_instance(self, queryset, instance): | |
| If an instance is being updated, then do not include | ||
| that instance itself as a uniqueness conflict. | ||
| """ | ||
| if instance is not None: | ||
| if instance is not None and isinstance(instance, Model): | ||
| return queryset.exclude(pk=instance.pk) | ||
| return queryset | ||
|
|
||
|
|
@@ -137,22 +137,20 @@ def enforce_required_fields(self, attrs, serializer): | |
| if missing_items: | ||
| raise ValidationError(missing_items, code='required') | ||
|
|
||
| def filter_queryset(self, attrs, queryset, serializer): | ||
| def filter_queryset(self, attrs, queryset, instance): | ||
| """ | ||
| Filter the queryset to all instances matching the given attributes. | ||
| """ | ||
| # field names => field sources | ||
| sources = [ | ||
| serializer.fields[field_name].source | ||
| for field_name in self.fields | ||
| ] | ||
| # field names => field sources; resolved by __call__ when available, | ||
| # otherwise fall back to self.fields for direct callers. | ||
| sources = getattr(self, '_sources', None) or list(self.fields) | ||
|
|
||
| # If this is an update, then any unprovided field should | ||
| # have it's value set based on the existing instance attribute. | ||
| if serializer.instance is not None: | ||
| if instance is not None: | ||
| for source in sources: | ||
| if source not in attrs: | ||
| attrs[source] = getattr(serializer.instance, source) | ||
| attrs[source] = getattr(instance, source) | ||
|
|
||
| # Determine the filter keyword arguments and filter the queryset. | ||
| filter_kwargs = { | ||
|
|
@@ -166,35 +164,56 @@ def exclude_current_instance(self, attrs, queryset, instance): | |
| If an instance is being updated, then do not include | ||
| that instance itself as a uniqueness conflict. | ||
| """ | ||
| if instance is not None: | ||
| if instance is not None and isinstance(instance, Model): | ||
| return queryset.exclude(pk=instance.pk) | ||
| return queryset | ||
|
|
||
| def __call__(self, attrs, serializer): | ||
| # When many=True is used, the parent ListSerializer's queryset is | ||
| # propagated as the child's instance. Treat that as a create so that | ||
| # per-object uniqueness checks don't try to call .pk / field attrs on | ||
| # the queryset. | ||
| instance = serializer.instance if isinstance(serializer.instance, Model) else None | ||
| self._sources = [ | ||
| serializer.fields[field_name].source | ||
| for field_name in self.fields | ||
| ] | ||
|
Comment on lines
+176
to
+180
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @LeSingh1 did you check this? |
||
|
|
||
| self.enforce_required_fields(attrs, serializer) | ||
|
|
||
| # When many=True is used, instance is normalized to None above. If the | ||
| # serializer is also partial, some unique-together fields may be absent | ||
| # from attrs; a uniqueness check is not meaningful in that case. | ||
| if instance is None and serializer.partial: | ||
| if any( | ||
| serializer.fields[field_name].source not in attrs | ||
| for field_name in self.fields | ||
| ): | ||
| return | ||
|
|
||
| queryset = self.queryset | ||
| queryset = self.filter_queryset(attrs, queryset, serializer) | ||
| queryset = self.exclude_current_instance(attrs, queryset, serializer.instance) | ||
| queryset = self.filter_queryset(attrs, queryset, instance) | ||
| queryset = self.exclude_current_instance(attrs, queryset, instance) | ||
|
|
||
| checked_names = [ | ||
| serializer.fields[field_name].source for field_name in self.fields | ||
| ] | ||
| # Ignore validation if any field is None | ||
| if serializer.instance is None: | ||
| if instance is None: | ||
| checked_values = [attrs[field_name] for field_name in checked_names] | ||
| else: | ||
| # Ignore validation if all field values are unchanged | ||
| checked_values = [ | ||
| attrs[field_name] | ||
| for field_name in checked_names | ||
| if attrs[field_name] != getattr(serializer.instance, field_name) | ||
| if attrs[field_name] != getattr(instance, field_name) | ||
| ] | ||
|
|
||
| condition_sources = (serializer.fields[field_name].source for field_name in self.condition_fields) | ||
| condition_kwargs = { | ||
| source: attrs[source] | ||
| if source in attrs | ||
| else getattr(serializer.instance, source) | ||
| else getattr(instance, source) | ||
| for source in condition_sources | ||
| } | ||
| if checked_values and None not in checked_values and qs_exists_with_condition(queryset, self.condition, condition_kwargs): | ||
|
|
@@ -273,7 +292,7 @@ def exclude_current_instance(self, attrs, queryset, instance): | |
| If an instance is being updated, then do not include | ||
| that instance itself as a uniqueness conflict. | ||
| """ | ||
| if instance is not None: | ||
| if instance is not None and isinstance(instance, Model): | ||
| return queryset.exclude(pk=instance.pk) | ||
| return queryset | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LeSingh1 and this?