def to_internal_value(self, data): if isinstance(data, six.text_type): try: data = json.loads(data) except ValueError: # show a useful error if they send a `pk` instead of resource object self.fail('incorrect_type', data_type=type(data).__name__) if not isinstance(data, dict): self.fail('incorrect_type', data_type=type(data).__name__) expected_relation_type = get_resource_type_from_queryset( self.get_queryset()) serializer_resource_type = self.get_resource_type_from_included_serializer( ) if serializer_resource_type is not None: expected_relation_type = serializer_resource_type if 'type' not in data: self.fail('missing_type') if 'id' not in data: self.fail('missing_id') if data['type'] != expected_relation_type: self.conflict('incorrect_relation_type', relation_type=expected_relation_type, received_type=data['type']) return super(ResourceRelatedField, self).to_internal_value(data['id'])
def to_internal_value(self, data): if isinstance(data, six.text_type): try: data = json.loads(data) except ValueError: # show a useful error if they send a `pk` instead of resource object self.fail('incorrect_type', data_type=type(data).__name__) if not isinstance(data, dict): self.fail('incorrect_type', data_type=type(data).__name__) expected_relation_type = get_resource_type_from_queryset(self.queryset) if 'type' not in data: self.fail('missing_type') if 'id' not in data: self.fail('missing_id') if data['type'] != expected_relation_type: self.conflict('incorrect_relation_type', relation_type=expected_relation_type, received_type=data['type']) try: return self.get_queryset().get(uuid=data['id']) except ObjectDoesNotExist: self.fail('does_not_exist', pk_value=data) except (TypeError, ValueError): self.fail('incorrect_type', data_type=type(data).__name__)
def to_internal_value(self, data): if isinstance(data, str): try: data = json.loads(data) except ValueError: # show a useful error if they send a `pk` instead of resource object self.fail("incorrect_type", data_type=type(data).__name__) if not isinstance(data, dict): self.fail("incorrect_type", data_type=type(data).__name__) expected_relation_type = get_resource_type_from_queryset( self.get_queryset()) serializer_resource_type = self.get_resource_type_from_included_serializer( ) if serializer_resource_type is not None: expected_relation_type = serializer_resource_type if "type" not in data: self.fail("missing_type") if "id" not in data: self.fail("missing_id") if data["type"] != expected_relation_type: self.conflict( "incorrect_relation_type", relation_type=expected_relation_type, received_type=data["type"], ) return super().to_internal_value(data["id"])
def to_internal_value(self, data): if isinstance(data, six.text_type): try: data = json.loads(data) except ValueError: # show a useful error if they send a `pk` instead of resource object self.fail('incorrect_type', data_type=type(data).__name__) if not isinstance(data, dict): self.fail('incorrect_type', data_type=type(data).__name__) expected_relation_type = get_resource_type_from_queryset(self.queryset) if data['type'] != expected_relation_type: self.conflict('incorrect_relation_type', relation_type=expected_relation_type, received_type=data['type']) return super(ResourceRelatedField, self).to_internal_value(data['id'])