本文整理汇总了Python中mongoengine.ReferenceField方法的典型用法代码示例。如果您正苦于以下问题:Python mongoengine.ReferenceField方法的具体用法?Python mongoengine.ReferenceField怎么用?Python mongoengine.ReferenceField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoengine
的用法示例。
在下文中一共展示了mongoengine.ReferenceField方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: models
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import ReferenceField [as 别名]
def models():
class HeadTeacher(me.EmbeddedDocument):
full_name = me.StringField(max_length=255, unique=True, default='noname')
class Course(me.Document):
id = me.IntField(primary_key=True)
name = me.StringField()
# These are for better model form testing
cost = me.IntField()
description = me.StringField()
level = me.StringField(choices=('Primary', 'Secondary'))
prereqs = me.DictField()
started = me.DateTimeField()
grade = AnotherIntegerField()
students = me.ListField(me.ReferenceField('Student'))
class School(me.Document):
name = me.StringField()
students = me.ListField(me.ReferenceField('Student'))
headteacher = me.EmbeddedDocumentField(HeadTeacher)
class Student(me.Document):
full_name = me.StringField(max_length=255, unique=True, default='noname')
age = me.IntField(min_value=10, max_value=99)
dob = me.DateTimeField(null=True)
date_created = me.DateTimeField(default=dt.datetime.utcnow,
help_text='date the student was created')
current_school = me.ReferenceField('School')
courses = me.ListField(me.ReferenceField('Course'))
email = me.EmailField(max_length=100)
profile_uri = me.URLField(max_length=200)
# So that we can access models with dot-notation, e.g. models.Course
class _models(object):
def __init__(self):
self.HeadTeacher = HeadTeacher
self.Course = Course
self.School = School
self.Student = Student
return _models()
示例2: reference_args
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import ReferenceField [as 别名]
def reference_args(self):
def get_reference_field(r, kv):
field = kv[1]
mongo_field = getattr(self.model, kv[0], None)
if isinstance(
mongo_field,
(mongoengine.LazyReferenceField, mongoengine.ReferenceField),
):
field = convert_mongoengine_field(mongo_field, self.registry)
if callable(getattr(field, "get_type", None)):
_type = field.get_type()
if _type:
node = _type._type._meta
if "id" in node.fields and not issubclass(
node.model, (mongoengine.EmbeddedDocument,)
):
r.update({kv[0]: node.fields["id"]._type.of_type()})
return r
return reduce(get_reference_field, self.fields.items(), {})
示例3: test_ReferenceField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import ReferenceField [as 别名]
def test_ReferenceField(self):
class ReferenceDoc(me.Document):
field = me.IntField(primary_key=True, default=42)
class Doc(me.Document):
id = me.StringField(primary_key=True, default='main')
ref = me.ReferenceField(ReferenceDoc)
fields_ = fields_for_model(Doc)
assert type(fields_['ref']) is fields.Reference
class DocSchema(ModelSchema):
class Meta:
model = Doc
ref_doc = ReferenceDoc().save()
doc = Doc(ref=ref_doc)
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'ref': 42, 'id': 'main'}
# Try the same with reference document type passed as string
class DocSchemaRefAsString(Schema):
id = fields.String()
ref = fields.Reference('ReferenceDoc')
dump = DocSchemaRefAsString().dump(doc)
assert not dump.errors
assert dump.data == {'ref': 42, 'id': 'main'}
# Test the field loading
load = DocSchemaRefAsString().load(dump.data)
assert not load.errors
assert type(load.data['ref']) == ReferenceDoc
# Try invalid loads
for bad_ref in (1, 'NaN', None):
dump.data['ref'] = bad_ref
_, errors = DocSchemaRefAsString().load(dump.data)
assert errors, bad_ref
示例4: convert_field_to_list
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import ReferenceField [as 别名]
def convert_field_to_list(field, registry=None):
base_type = convert_mongoengine_field(field.field, registry=registry)
if isinstance(base_type, graphene.Field):
return graphene.List(
base_type._type,
description=get_field_description(field, registry),
required=field.required
)
if isinstance(base_type, (graphene.Dynamic)):
base_type = base_type.get_type()
if base_type is None:
return
base_type = base_type._type
if graphene.is_node(base_type):
return base_type._meta.connection_field_class(base_type)
# Non-relationship field
relations = (mongoengine.ReferenceField, mongoengine.EmbeddedDocumentField)
if not isinstance(base_type, (graphene.List, graphene.NonNull)) and not isinstance(
field.field, relations
):
base_type = type(base_type)
return graphene.List(
base_type,
description=get_field_description(field, registry),
required=field.required,
)
示例5: convert_field_to_union
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import ReferenceField [as 别名]
def convert_field_to_union(field, registry=None):
_types = []
for choice in field.choices:
if isinstance(field, mongoengine.GenericReferenceField):
_field = mongoengine.ReferenceField(get_document(choice))
elif isinstance(field, mongoengine.GenericEmbeddedDocumentField):
_field = mongoengine.EmbeddedDocumentField(choice)
_field = convert_mongoengine_field(_field, registry)
_type = _field.get_type()
if _type:
_types.append(_type.type)
else:
# TODO: Register type auto-matically here.
pass
if len(_types) == 0:
return None
# XXX: Use uuid to avoid duplicate name
name = "{}_{}_union_{}".format(
field._owner_document.__name__,
field.db_field,
str(uuid.uuid1()).replace("-", ""),
)
Meta = type("Meta", (object,), {"types": tuple(_types)})
_union = type(name, (graphene.Union,), {"Meta": Meta})
return graphene.Field(_union)