Update WritableNestedSerializer to call unrestricted() on RestrictedQuerySets

This commit is contained in:
Jeremy Stretch 2020-06-29 10:38:32 -04:00
parent 0dbe248df8
commit a6b03b8884

View File

@ -254,8 +254,11 @@ class WritableNestedSerializer(ModelSerializer):
# Dictionary of related object attributes # Dictionary of related object attributes
if isinstance(data, dict): if isinstance(data, dict):
params = dict_to_filter_params(data) params = dict_to_filter_params(data)
queryset = self.Meta.model.objects
if hasattr(queryset, 'restrict'):
queryset = queryset.unrestricted()
try: try:
return self.Meta.model.objects.get(**params) return queryset.get(**params)
except ObjectDoesNotExist: except ObjectDoesNotExist:
raise ValidationError( raise ValidationError(
"Related object not found using the provided attributes: {}".format(params) "Related object not found using the provided attributes: {}".format(params)
@ -281,8 +284,11 @@ class WritableNestedSerializer(ModelSerializer):
) )
# Look up object by PK # Look up object by PK
queryset = self.Meta.model.objects
if hasattr(queryset, 'restrict'):
queryset = queryset.unrestricted()
try: try:
return self.Meta.model.objects.get(pk=int(data)) return queryset.get(pk=int(data))
except ObjectDoesNotExist: except ObjectDoesNotExist:
raise ValidationError( raise ValidationError(
"Related object not found using the provided numeric ID: {}".format(pk) "Related object not found using the provided numeric ID: {}".format(pk)