当前位置: 首页>>代码示例>>Python>>正文


Python mongoengine.StringField方法代码示例

本文整理汇总了Python中mongoengine.StringField方法的典型用法代码示例。如果您正苦于以下问题:Python mongoengine.StringField方法的具体用法?Python mongoengine.StringField怎么用?Python mongoengine.StringField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mongoengine的用法示例。


在下文中一共展示了mongoengine.StringField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_invalid_state_transition

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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() 
开发者ID:jtushman,项目名称:state_machine,代码行数:23,代码来源:tests.py

示例2: test_before_callback_blocking_transition

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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 
开发者ID:jtushman,项目名称:state_machine,代码行数:26,代码来源:tests.py

示例3: test_FileField

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:18,代码来源:test_fields.py

示例4: test_ListField

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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_ 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:18,代码来源:test_fields.py

示例5: test_ListSpecialField

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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'] 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:22,代码来源:test_fields.py

示例6: test_GenericEmbeddedDocumentField

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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 ? 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:25,代码来源:test_fields.py

示例7: test_MapField

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:24,代码来源:test_fields.py

示例8: models

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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() 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:44,代码来源:test_marshmallow_mongoengine.py

示例9: test_required

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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' 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:21,代码来源:test_params.py

示例10: test_default

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [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' 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:23,代码来源:test_params.py

示例11: extra_class_members

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [as 别名]
def extra_class_members(self, initial_state):
        return {'aasm_state': mongoengine.StringField(default=initial_state.name)} 
开发者ID:jtushman,项目名称:state_machine,代码行数:4,代码来源:mongoengine.py

示例12: test_skip_none_field

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [as 别名]
def test_skip_none_field(self):
        class Doc(me.Document):
            field_not_empty = me.StringField(default='value')
            field_empty = me.StringField()
            list_empty = me.ListField(me.StringField())
        class DocSchema(ModelSchema):
            class Meta:
                model = Doc
        doc = Doc()
        dump = DocSchema().dump(doc)
        assert not dump.errors
        assert dump.data == {'field_not_empty': 'value'} 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:14,代码来源:test_skip.py

示例13: test_disable_skip_none_field

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [as 别名]
def test_disable_skip_none_field(self):
        class Doc(me.Document):
            field_empty = me.StringField()
            list_empty = me.ListField(me.StringField())
        class DocSchema(ModelSchema):
            class Meta:
                model = Doc
                model_skip_values = ()
        doc = Doc()
        data, errors = DocSchema().dump(doc)
        assert not errors
        assert data == {'field_empty': None, 'list_empty': []} 
开发者ID:touilleMan,项目名称:marshmallow-mongoengine,代码行数:14,代码来源:test_skip.py

示例14: test_should_string_convert_string

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [as 别名]
def test_should_string_convert_string():
    assert_conversion(mongoengine.StringField, graphene.String) 
开发者ID:graphql-python,项目名称:graphene-mongo,代码行数:4,代码来源:test_converter.py

示例15: test_should_map_convert_json

# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import StringField [as 别名]
def test_should_map_convert_json():
    assert_conversion(
        mongoengine.MapField, graphene.JSONString, field=mongoengine.StringField()
    ) 
开发者ID:graphql-python,项目名称:graphene-mongo,代码行数:6,代码来源:test_converter.py


注:本文中的mongoengine.StringField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。