code: def value_to_datum(self, instance, value):
          
          if value is None:
              return None
          bound = getattr(instance._origin, self.cls)
          if type(value) is bound:
              if self.use_data_setter:
                  # Using data setter, so just return the data for the object.
                  return value._data
              else:
                  # Use the primary keys to set the value.
                  descriptors, alt_descriptors = get_pk_descriptors(bound)
                  if len(descriptors) == 1:
                      return getattr(value, descriptors[0][0])
                  elif len(descriptors) > 1:
                      return tuple(
                          getattr(value, name)
                          for name, _ in descriptors
                      )
                  else:
                      raise AttributeError(
                          "unable to perform set object no primary key "
                          "fields defined for %s" % self.cls)
          else:
              raise TypeError(
                  "must be %s, not %s" % (self.cls, type(value).__name__))
docs: Convert a given Python-side value to a MAAS-side datum.
  
          :param instance: The `Object` instance on which this field is
              currently operating. This method should treat it as read-only, for
              example to perform validation with regards to other fields.
          :param datum: The Python-side value to validate and convert into a
              MAAS-side datum.
          :return: A datum derived from the given value.