code: def validate(self, value):
          
          if value == '':
              if self.kwargs.get('nullable', __nullable__):
                  value = None
              else:
                  value = 0
              
          if not isinstance(value, Model):
              return super(ReferenceProperty, self).validate(value)
  
          if not value.is_saved():
              raise BadValueError(
                      '%s instance must be saved before it can be stored as a '
                      'reference' % self.reference_class.__class__.__name__)
          if not isinstance(value, self.reference_class):
              raise KindError('Property %s must be an instance of %s' %
                      (self.name, self.reference_class.__class__.__name__))
  
          return value
docs: Validate reference.
  
          Returns:
              A valid value.
  
          Raises:
              BadValueError for the following reasons:
                  - Value is not saved.
                  - Object not of correct model type for reference.