本文整理汇总了Python中mongoengine.Document方法的典型用法代码示例。如果您正苦于以下问题:Python mongoengine.Document方法的具体用法?Python mongoengine.Document怎么用?Python mongoengine.Document使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoengine
的用法示例。
在下文中一共展示了mongoengine.Document方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_invalid_state_transition
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_invalid_state_transition():
@acts_as_state_machine
class Person(mongoengine.Document):
name = mongoengine.StringField(default='Billy')
sleeping = State(initial=True)
running = State()
cleaning = State()
run = Event(from_states=sleeping, to_state=running)
cleanup = Event(from_states=running, to_state=cleaning)
sleep = Event(from_states=(running, cleaning), to_state=sleeping)
establish_mongo_connection()
person = Person()
person.save()
assert person.is_sleeping
#should raise an invalid state exception
with assert_raises(InvalidStateTransition):
person.sleep()
示例2: test_before_callback_blocking_transition
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_before_callback_blocking_transition():
@acts_as_state_machine
class Runner(mongoengine.Document):
name = mongoengine.StringField(default='Billy')
sleeping = State(initial=True)
running = State()
cleaning = State()
run = Event(from_states=sleeping, to_state=running)
cleanup = Event(from_states=running, to_state=cleaning)
sleep = Event(from_states=(running, cleaning), to_state=sleeping)
@before('run')
def check_sneakers(self):
return False
establish_mongo_connection()
runner = Runner()
runner.save()
assert runner.is_sleeping
runner.run()
assert runner.is_sleeping
assert not runner.is_running
示例3: test_model_mismatch
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_model_mismatch(self):
""" cannot apply filters for one model to queryset from another """
class FooDoc(Document):
foo = fields.StringField()
class BarDoc(Document):
bar = fields.StringField()
class FooFilter(ModelFilterset):
class Meta:
model = FooDoc
class BarView(ListAPIView):
filter_backends = (MongoFilterBackend,)
filter_class = FooFilter
serializer_class = mock.Mock()
queryset = BarDoc.objects
with self.assertRaises(TypeError):
BarView.as_view()(APIRequestFactory().get("/?foo=Foo"))
示例4: test_model_subclassed
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_model_subclassed(self):
""" can apply filters for base model to queryset of derived """
class FooDoc(Document):
meta = { 'allow_inheritance': True}
foo = fields.StringField()
class BarDoc(FooDoc):
bar = fields.StringField()
class FooFilter(ModelFilterset):
class Meta:
model = FooDoc
class BarView(ListAPIView):
filter_backends = (MongoFilterBackend,)
filter_class = FooFilter
serializer_class = mock.Mock()
queryset = BarDoc.objects
BarView.as_view()(APIRequestFactory().get("/?foo=Foo"))
示例5: test_excluding_declared
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_excluding_declared(self):
class MockModel(Document):
foo = fields.StringField()
bar = fields.StringField()
baz = fields.StringField()
class BaseFS(ModelFilterset):
class Meta:
model = MockModel
foo = filters.CharFilter()
bar = filters.CharFilter()
class TestFS(BaseFS):
class Meta:
model = MockModel
exclude = ('bar', 'baz')
baz = filters.CharFilter()
quz = filters.CharFilter()
fs = TestFS()
self.assertEqual(set(fs.filters.keys()), set(['id', 'foo', 'quz']))
示例6: __init__
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def __init__(self, items, links=None): # , session):
self.forest = {} # id -> (event/result, parent id, rank)
"""
:type items: List[Document]
"""
if links is None:
links = lambda x: x.links
self._get_neighbors = links
# self.session = session
for item in items:
self.forest[item.id] = (item, item.id, 0)
# generate sets
for item in items:
for neighbor in links(item):
self.union(item.id, neighbor.id)
# for key, (item, item_id, index) in self.forest.items():
# for neighbor in self._get_neighbors(item):
# self.union(key, neighbor.id)
# recursively search until you find a parent who is itself
示例7: test_FileField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_FileField(self):
class File(me.Document):
name = me.StringField(primary_key=True)
file = me.FileField()
class FileSchema(ModelSchema):
class Meta:
model = File
doc = File(name='test_file')
data = b'1234567890' * 10
doc.file.put(data, content_type='application/octet-stream')
dump = FileSchema().dump(doc)
assert not dump.errors
assert dump.data == {'name': 'test_file'}
# Should not be able to load the file
load = FileSchema().load({'name': 'bad_load', 'file': b'12345'})
assert not load.data.file
示例8: test_ListField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_ListField(self):
class Doc(me.Document):
list = me.ListField(me.StringField())
fields_ = fields_for_model(Doc)
assert type(fields_['list']) is fields.List
class DocSchema(ModelSchema):
class Meta:
model = Doc
list_ = ['A', 'B', 'C']
doc = Doc(list=list_)
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'list': list_}
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.list == list_
示例9: test_ListSpecialField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_ListSpecialField(self):
class NestedDoc(me.EmbeddedDocument):
field = me.StringField()
class Doc(me.Document):
list = me.ListField(me.EmbeddedDocumentField(NestedDoc))
fields_ = fields_for_model(Doc)
assert type(fields_['list']) is fields.List
assert type(fields_['list'].container) is fields.Nested
class DocSchema(ModelSchema):
class Meta:
model = Doc
list_ = [{'field': 'A'}, {'field': 'B'}, {'field': 'C'}]
doc = Doc(list=list_)
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'list': list_}
load = DocSchema().load(dump.data)
assert not load.errors
for i, elem in enumerate(list_):
assert load.data.list[i].field == elem['field']
示例10: test_DictField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_DictField(self):
class Doc(me.Document):
data = me.DictField()
fields_ = fields_for_model(Doc)
assert type(fields_['data']) is fields.Raw
class DocSchema(ModelSchema):
class Meta:
model = Doc
data = {
'int_1': 1,
'nested_2': {
'sub_int_1': 42,
'sub_list_2': []
},
'list_3': ['a', 'b', 'c']
}
doc = Doc(data=data)
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'data': data}
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.data == data
示例11: test_DynamicField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_DynamicField(self):
class Doc(me.Document):
dynamic = me.DynamicField()
fields_ = fields_for_model(Doc)
assert type(fields_['dynamic']) is fields.Raw
class DocSchema(ModelSchema):
class Meta:
model = Doc
data = {
'int_1': 1,
'nested_2': {
'sub_int_1': 42,
'sub_list_2': []
},
'list_3': ['a', 'b', 'c']
}
doc = Doc(dynamic=data)
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data == {'dynamic': data}
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.dynamic == data
示例12: test_MapField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [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
示例13: test_PointField
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_PointField(self):
class Doc(me.Document):
point = me.PointField()
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc = Doc(point={ 'type': 'Point', 'coordinates': [10, 20] })
dump = DocSchema().dump(doc)
assert not dump.errors
assert dump.data['point'] == { 'x': 10, 'y': 20 }
load = DocSchema().load(dump.data)
assert not load.errors
assert load.data.point == { 'type': 'Point', 'coordinates': [10, 20] }
# Deserialize Point with coordinates passed as string
data = {'point': { 'x': '10', 'y': '20' }}
load = DocSchema().load(data)
assert not load.errors
assert load.data.point == { 'type': 'Point', 'coordinates': [10, 20] }
# Try to load invalid coordinates
data = {'point': { 'x': '10', 'y': '20foo' }}
load = DocSchema().load(data)
assert 'point' in load.errors
示例14: test_required
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_required(self):
class Doc(me.Document):
field_not_required = me.StringField()
field_required = me.StringField(required=True)
class DocSchema(ModelSchema):
class Meta:
model = Doc
doc, errors = DocSchema().load({'field_not_required': 'bad_doc'})
assert errors
# Now provide the required field
doc, errors = DocSchema().load({'field_required': 'good_doc'})
assert not errors
assert doc.field_not_required is None
assert doc.field_required == 'good_doc'
# Update should not take care of the required fields
doc, errors = DocSchema().update(doc, {'field_not_required': 'good_doc'})
assert not errors
assert doc.field_required == 'good_doc'
assert doc.field_not_required == 'good_doc'
示例15: test_default
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import Document [as 别名]
def test_default(self):
def generate_default_value():
return 'default_generated_value'
class Doc(me.Document):
field_with_default = me.StringField(default='default_value')
field_required_with_default = me.StringField(required=True,
default=generate_default_value)
class DocSchema(ModelSchema):
class Meta:
model = Doc
# Make sure default doesn't shadow given values
doc, errors = DocSchema().load({'field_with_default': 'custom_value',
'field_required_with_default': 'custom_value'})
assert not errors
assert doc.field_with_default == 'custom_value'
assert doc.field_required_with_default == 'custom_value'
# Now use defaults
doc, errors = DocSchema().load({})
assert not errors
assert doc.field_with_default == 'default_value'
assert doc.field_required_with_default == 'default_generated_value'