def _key_to_index(self, key):
        """
        Try to find the index at which the key resides.
        We're using index here instead of a mapping, trading efficiency for
        avoiding the added complexity of managing changes to the primary_key.

        May return an invalid index.
        """
        if not isinstance(key, slice):
            try:
                if not isinstance(key, self.model):
                    return self.index((self.model, key))
                return self.index(key)
            except ValueError:
                pass
        return key

        Try to find the index at which the key resides.
        We're using index here instead of a mapping, trading efficiency for
        avoiding the added complexity of managing changes to the primary_key.

        May return an invalid index.
        
portia_server/portia_orm/collection.py