          'does_not_exist': _("Invalid pk '{pk_value}' - object does not exist."),
          'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'),
      }
  
      def to_internal_value(self, data):
          try:
              return self.get_queryset().get(pk=data)
          except ObjectDoesNotExist:
              self.fail('does_not_exist', pk_value=data)
          except (TypeError, ValueError):
 
</s> remove     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
        ]

 </s> add  </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
    def use_pk_only_optimization(self):
        return True

          'does_not_exist': _("Invalid pk '{pk_value}' - object does not exist."),
          'incorrect_type': _('Incorrect type. Expected pk value, received {data_type}.'),
      }
  
    def use_pk_only_optimization(self):
        return True

      def to_internal_value(self, data):
          try:
              return self.get_queryset().get(pk=data)
          except ObjectDoesNotExist:
              self.fail('does_not_exist', pk_value=data)
          except (TypeError, ValueError):
keep keep keep add keep keep keep keep keep keep
 Hyperlinked PK optimization. Closes #1872. 
rest_framework/relations.py