              self.fail('does_not_exist', pk_value=data)
          except (TypeError, ValueError):
              self.fail('incorrect_type', data_type=type(data).__name__)
  
      def get_attribute(self, instance):
          # We customize `get_attribute` here for performance reasons.
          # For relationships the instance will already have the pk of
          # the related object. We return this directly instead of returning the
          # object itself, which would require a database lookup.
          try:
              instance = get_attribute(instance, self.source_attrs[:-1])
              return PKOnlyObject(pk=instance.serializable_value(self.source_attrs[-1]))
          except AttributeError:
              return get_attribute(instance, self.source_attrs)
  
      def get_iterable(self, instance, source_attrs):
          # For consistency with `get_attribute` we're using `serializable_value()`
          # here. Typically there won't be any difference, but some custom field
          # types might return a non-primitive value for the pk otherwise.
          #
          # We could try to get smart with `values_list('pk', flat=True)`, which
          # would be better in some case, but would actually end up with *more*
          # queries if the developer is using `prefetch_related` across the
          # relationship.
          relationship = super(PrimaryKeyRelatedField, self).get_iterable(instance, source_attrs)
          return [
              PKOnlyObject(pk=item.serializable_value('pk'))
              for item in relationship
          ]
  
      def to_representation(self, value):
          return value.pk
  
  
  class HyperlinkedRelatedField(RelatedField):
 
</s> remove         return relationship.all() if (hasattr(relationship, 'all')) else relationship
 </s> add         relationship = relationship.all() if (hasattr(relationship, 'all')) else relationship

        if self.use_pk_only_optimization():
            # Optimized case, return mock objects only containing the pk attribute.
            return [
                PKOnlyObject(pk=pk)
                for pk in relationship.values_list('pk', flat=True)
            ]

        # Standard case, return the object instances.
        return relationship
              self.fail('does_not_exist', pk_value=data)
          except (TypeError, ValueError):
              self.fail('incorrect_type', data_type=type(data).__name__)
  


























      def to_representation(self, value):
          return value.pk
  
  
  class HyperlinkedRelatedField(RelatedField):
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Hyperlinked PK optimization. Closes #1872. 
rest_framework/relations.py