mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Merge 3866a1be70
into 5aa204e94f
This commit is contained in:
commit
6e99413f75
|
@ -7,6 +7,8 @@ object creation, and makes it possible to switch between using the implicit
|
||||||
`ModelSerializer` class and an equivalent explicit `Serializer` class.
|
`ModelSerializer` class and an equivalent explicit `Serializer` class.
|
||||||
"""
|
"""
|
||||||
from __future__ import unicode_literals
|
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 django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework.compat import unicode_to_repr
|
from rest_framework.compat import unicode_to_repr
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
|
@ -112,7 +114,7 @@ class UniqueTogetherValidator(object):
|
||||||
"""
|
"""
|
||||||
# If this is an update, then any unprovided field should
|
# If this is an update, then any unprovided field should
|
||||||
# have it's value set based on the existing instance attribute.
|
# 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:
|
for field_name in self.fields:
|
||||||
if field_name not in attrs:
|
if field_name not in attrs:
|
||||||
attrs[field_name] = getattr(self.instance, field_name)
|
attrs[field_name] = getattr(self.instance, field_name)
|
||||||
|
@ -121,6 +123,7 @@ class UniqueTogetherValidator(object):
|
||||||
filter_kwargs = dict([
|
filter_kwargs = dict([
|
||||||
(field_name, attrs[field_name])
|
(field_name, attrs[field_name])
|
||||||
for field_name in self.fields
|
for field_name in self.fields
|
||||||
|
if field_name in attrs
|
||||||
])
|
])
|
||||||
return queryset.filter(**filter_kwargs)
|
return queryset.filter(**filter_kwargs)
|
||||||
|
|
||||||
|
@ -130,6 +133,9 @@ class UniqueTogetherValidator(object):
|
||||||
that instance itself as a uniqueness conflict.
|
that instance itself as a uniqueness conflict.
|
||||||
"""
|
"""
|
||||||
if self.instance is not None:
|
if self.instance is not None:
|
||||||
|
if isinstance(self.instance, QuerySet):
|
||||||
|
return queryset.exclude(pk__in=self.instance.all())
|
||||||
|
else:
|
||||||
return queryset.exclude(pk=self.instance.pk)
|
return queryset.exclude(pk=self.instance.pk)
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user