Allow serializer data to include related objects by object

When instantiating a serializer from cleaned data, the relational fields
have been already converted into objects, which makes the fields
invalid. Having a valid object as the value of a field should be
considered valid, even if not in the prescribed format.

As such, this change adds a check to allow objects of the model type in
lieu of the proper representation.
This commit is contained in:
Étienne Beaulé 2020-03-05 20:13:15 -05:00
parent 4a98533746
commit d5f895f6c0
No known key found for this signature in database
GPG Key ID: E29CABE72BECCB8E

View File

@ -250,6 +250,10 @@ class PrimaryKeyRelatedField(RelatedField):
return True
def to_internal_value(self, data):
if isinstance(data, self.get_queryset().model):
return data
if self.pk_field is not None:
data = self.pk_field.to_internal_value(data)
try:
@ -331,6 +335,10 @@ class HyperlinkedRelatedField(RelatedField):
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
def to_internal_value(self, data):
if isinstance(data, self.get_queryset().model):
return data
request = self.context.get('request', None)
try:
http_prefix = data.startswith(('http:', 'https:'))