Skip to content
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

Fix in UniqueTogetherValidator to allow it to handle querysets #2575

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 8 additions & 2 deletions rest_framework/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
`ModelSerializer` class and an equivalent explicit `Serializer` class.
"""
from __future__ import unicode_literals
from django.db import models
from django.db.models.query import QuerySet
from django.utils.translation import ugettext_lazy as _
from rest_framework.compat import unicode_to_repr
from rest_framework.exceptions import ValidationError
Expand Down Expand Up @@ -112,7 +114,7 @@ def filter_queryset(self, attrs, queryset):
"""
# If this is an update, then any unprovided field should
# have it's value set based on the existing instance attribute.
if self.instance is not None:
if self.instance is not None and isinstance(self.instance, models.Model):
for field_name in self.fields:
if field_name not in attrs:
attrs[field_name] = getattr(self.instance, field_name)
Expand All @@ -121,6 +123,7 @@ def filter_queryset(self, attrs, queryset):
filter_kwargs = dict([
(field_name, attrs[field_name])
for field_name in self.fields
if field_name in attrs
])
return queryset.filter(**filter_kwargs)

Expand All @@ -130,7 +133,10 @@ def exclude_current_instance(self, attrs, queryset):
that instance itself as a uniqueness conflict.
"""
if self.instance is not None:
return queryset.exclude(pk=self.instance.pk)
if isinstance(self.instance, QuerySet):
return queryset.exclude(pk__in=self.instance.all())
else:
return queryset.exclude(pk=self.instance.pk)
return queryset

def __call__(self, attrs):
Expand Down