本文整理汇总了Python中mongoengine.IntField方法的典型用法代码示例。如果您正苦于以下问题:Python mongoengine.IntField方法的具体用法?Python mongoengine.IntField怎么用?Python mongoengine.IntField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoengine
的用法示例。
在下文中一共展示了mongoengine.IntField方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_GenericEmbeddedDocumentField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [as 别名]
def test_GenericEmbeddedDocumentField(self):
class Doc(me.Document):
id = me.StringField(primary_key=True, default='main')
embedded = me.GenericEmbeddedDocumentField()
class EmbeddedA(me.EmbeddedDocument):
field_a = me.StringField(default='field_a_value')
class EmbeddedB(me.EmbeddedDocument):
field_b = me.IntField(default=42)
fields_ = fields_for_model(Doc)
assert type(fields_['embedded']) is fields.GenericEmbeddedDocument
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc = Doc(embedded=EmbeddedA())
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'embedded': {'field_a': 'field_a_value'}, 'id': 'main'}
doc.embedded = EmbeddedB()
doc.save()
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'embedded': {'field_b': 42}, 'id': 'main'}
# TODO: test load ?
示例2: test_MapField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [as 别名]
def test_MapField(self):
class MappedDoc(me.EmbeddedDocument):
field = me.StringField()
class Doc(me.Document):
id = me.IntField(primary_key=True, default=1)
map = me.MapField(me.EmbeddedDocumentField(MappedDoc))
str = me.MapField(me.StringField())
fields_ = fields_for_model(Doc)
assert type(fields_['map']) is fields.Map
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc = Doc(map={'a': MappedDoc(field='A'), 'b': MappedDoc(field='B')},
str={'a': 'aaa', 'b': 'bbbb'}).save()
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'map': {'a': {'field': 'A'}, 'b': {'field': 'B'}},
'str': {'a': 'aaa', 'b': 'bbbb'}, 'id': 1}
# Try the load
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.map == doc.map
示例3: models
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [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()
示例4: test_ReferenceField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [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
示例5: test_required_with_default
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [as 别名]
def test_required_with_default(self):
class Doc(me.Document):
basic = me.IntField(required=True, default=42)
cunning = me.BooleanField(required=True, default=False)
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc, errors = DocSchema().load({})
assert not errors
assert doc.basic == 42
assert doc.cunning is False
示例6: test_sould_int_convert_int
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import IntField [as 别名]
def test_sould_int_convert_int():
assert_conversion(mongoengine.IntField, graphene.Int)