def __init__(self, type, db=None, **kwargs): from mongoalchemy.fields import DocumentField super(SRefField, self).__init__(**kwargs) self.type = type if not isinstance(type, DocumentField): self.type = DocumentField(type) self.db = db
def __init__(self, type=None, db=None, db_required=False, namespace='global', **kwargs): ''' :param type: (optional) the Field type to use for the values. It must be a DocumentField. If you want to save refs to raw mongo objects, you can leave this field out :param db: (optional) The database to load the object from. Defaults to the same database as the object this field is bound to. :param namespace: If using the namespace system and using a collection name instead of a type, selects which namespace to use ''' from mongoalchemy.fields import DocumentField if type and not isinstance(type, DocumentField): type = DocumentField(type) super(RefField, self).__init__(**kwargs) self.db_required = db_required self.type = type self.namespace = namespace self.db = db self.parent = None
class SRefField(RefBase): ''' A Simple RefField (SRefField) looks like an ObjectIdField in the database, but acts like a mongo DBRef. It uses the passed in type to determine where to look for the object (and assumes the current database). ''' has_subfields = True has_autoload = True def __init__(self, type, db=None, **kwargs): from mongoalchemy.fields import DocumentField super(SRefField, self).__init__(**kwargs) self.type = type if not isinstance(type, DocumentField): self.type = DocumentField(type) self.db = db def schema_json(self): super_schema = super(SRefField, self).schema_json() return dict(subtype=self.type.schema_json(), db=self.db, **super_schema) def _to_ref(self, doc): return doc.mongo_id def dereference(self, session, ref, allow_none=False): """ Dereference an ObjectID to this field's underlying type """ ref = DBRef(id=ref, collection=self.type.type.get_collection_name(), database=self.db) ref.type = self.type.type return session.dereference(ref, allow_none=allow_none) def set_parent_on_subtypes(self, parent): self.type.parent = parent def wrap(self, value): self.validate_wrap(value) return value def unwrap(self, value, fields=None, session=None): self.validate_unwrap(value) return value def validate_unwrap(self, value, session=None): if not isinstance(value, ObjectId): self._fail_validation_type(value, ObjectId) validate_wrap = validate_unwrap
class SRefField(RefBase): """ A Simple RefField (SRefField) looks like an ObjectIdField in the database, but acts like a mongo DBRef. It uses the passed in type to determine where to look for the object (and assumes the current database). """ has_subfields = True has_autoload = True def __init__(self, type, db=None, **kwargs): from mongoalchemy.fields import DocumentField super(SRefField, self).__init__(**kwargs) self.type = type if not isinstance(type, DocumentField): self.type = DocumentField(type) self.db = db def schema_json(self): super_schema = super(SRefField, self).schema_json() return dict(subtype=self.type.schema_json(), db=self.db, **super_schema) def _to_ref(self, doc): return doc.mongo_id def dereference(self, session, ref, allow_none=False): """ Dereference an ObjectID to this field's underlying type """ ref = DBRef(id=ref, collection=self.type.type.get_collection_name(), database=self.db) ref.type = self.type.type return session.dereference(ref, allow_none=allow_none) def set_parent_on_subtypes(self, parent): self.type.parent = parent def wrap(self, value): self.validate_wrap(value) return value def unwrap(self, value, fields=None, session=None): self.validate_unwrap(value) return value def validate_unwrap(self, value, session=None): if not isinstance(value, ObjectId): self._fail_validation_type(value, ObjectId) validate_wrap = validate_unwrap