本文整理匯總了Python中mongoengine.fields.BaseField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.BaseField方法的具體用法?Python fields.BaseField怎麽用?Python fields.BaseField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongoengine.fields
的用法示例。
在下文中一共展示了fields.BaseField方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_custom_type
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BaseField [as 別名]
def test_custom_type(self):
class FooField(fields.BaseField):
pass
class MockModel(Document):
foo = FooField()
class TestFS(ModelFilterset):
filters_mapping = {
FooField: filters.CharFilter
}
class Meta:
model = MockModel
fs = TestFS()
self.assertEqual(set(fs.filters.keys()), set(['id', 'foo']))
示例2: register
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BaseField [as 別名]
def register(self, key, dbtype):
'''Register a DB type to add constraint on a given extra key'''
if not issubclass(dbtype, (BaseField, EmbeddedDocument)):
msg = 'ExtrasField can only register MongoEngine fields'
raise TypeError(msg)
self.registered[key] = dbtype
示例3: test_validate_custom_type
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BaseField [as 別名]
def test_validate_custom_type(self):
class Tester(db.Document):
extras = db.ExtrasField()
@Tester.extras('test')
class Custom(BaseField):
def validate(self, value):
if not isinstance(value, dict):
raise db.ValidationError('Should be a dict instance')
tester = Tester(extras={'test': {}})
tester.validate()
示例4: smart_repr
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BaseField [as 別名]
def smart_repr(value):
if isinstance(value, QuerySet):
return manager_repr(value)
if isinstance(value, BaseField):
return mongo_field_repr(value)
if isinstance(value, BaseDocument):
return mongo_field_repr(value)
if isinstance(value, Field):
return field_repr(value)
value = repr(value)
# Representations like u'help text'
# should simply be presented as 'help text'
value = uni_lit_re.sub("'\\1'", value)
# Representations like
# <django.core.validators.RegexValidator object at 0x1047af050>
# Should be presented as
# <django.core.validators.RegexValidator object>
value = re.sub(' at 0x[0-9a-f]{4,32}>', '>', value)
return value