code: def to_foreign(self, obj, name, value):  # pylint:disable=unused-argument
  		
  		
  		if self.cache:
  			return self._populate_cache(value)
  		
  		identifier = value
  		
  		# First, we handle the typcial Document object case.
  		if isinstance(value, Document):
  			identifier = value.__data__.get('_id', None)
  			if identifier is None:
  				raise ValueError("Can only store a reference to a saved Document instance with an `_id` stored.")
  		
  		elif isinstance(value, (str, unicode)) and len(value) == 24:
  			try:
  				identifier = OID(value)
  			except InvalidId:
  				pass
  		
  		kind = self._kind(obj.__class__)
  		
  		if self.concrete:
  			if isinstance(value, Document) and value.__collection__:
  				return DBRef(value.__collection__, identifier)
  			
  			if getattr(kind, '__collection__', None):
  				return DBRef(kind.__collection__, identifier)
  			
  			raise ValueError("Could not infer collection name.")
  		
  		return identifier
docs: Transform to a MongoDB-safe value.