本文整理匯總了Python中mongoengine.fields.BooleanField方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.BooleanField方法的具體用法?Python fields.BooleanField怎麽用?Python fields.BooleanField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongoengine.fields
的用法示例。
在下文中一共展示了fields.BooleanField方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_regular_fields
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BooleanField [as 別名]
def test_regular_fields(self):
"""
Model fields should map to their equivelent serializer fields.
"""
class TestSerializer(DocumentSerializer):
class Meta:
model = RegularModel
fields = '__all__'
regex_repr = "re.compile('^valid_regex')"
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
str_field = CharField(required=False)
str_regex_field = RegexField(regex=%s, required=False)
url_field = URLField(required=False)
email_field = EmailField(required=False)
int_field = IntegerField(required=False)
long_field = IntegerField(required=False)
float_field = FloatField(required=False)
boolean_field = BooleanField(required=False)
nullboolean_field = NullBooleanField(required=False)
date_field = DateTimeField(required=False)
complexdate_field = DateTimeField(required=False)
uuid_field = UUIDField(required=False)
id_field = ObjectIdField(required=False)
decimal_field = DecimalField(decimal_places=2, max_digits=65536, required=False)
custom_field = DocumentField(model_field=<tests.test_basic.CustomField: custom_field>, required=False)
""" % regex_repr)
assert repr(TestSerializer()) == expected
示例2: test_meta_exclude
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BooleanField [as 別名]
def test_meta_exclude(self):
"""
Serializer should respect Meta.exclude
"""
class TestSerializer(DocumentSerializer):
class Meta:
model = RegularModel
exclude = ('decimal_field', 'custom_field')
regex_repr = "re.compile('^valid_regex')"
expected = dedent("""
TestSerializer():
id = ObjectIdField(read_only=True)
str_field = CharField(required=False)
str_regex_field = RegexField(regex=%s, required=False)
url_field = URLField(required=False)
email_field = EmailField(required=False)
int_field = IntegerField(required=False)
long_field = IntegerField(required=False)
float_field = FloatField(required=False)
boolean_field = BooleanField(required=False)
nullboolean_field = NullBooleanField(required=False)
date_field = DateTimeField(required=False)
complexdate_field = DateTimeField(required=False)
uuid_field = UUIDField(required=False)
id_field = ObjectIdField(required=False)
""" % regex_repr)
assert repr(TestSerializer()) == expected
示例3: build_standard_field
# 需要導入模塊: from mongoengine import fields [as 別名]
# 或者: from mongoengine.fields import BooleanField [as 別名]
def build_standard_field(self, field_name, model_field):
field_mapping = ClassLookupDict(self.serializer_field_mapping)
field_class = field_mapping[model_field]
field_kwargs = get_field_kwargs(field_name, model_field)
if 'choices' in field_kwargs:
# Fields with choices get coerced into `ChoiceField`
# instead of using their regular typed field.
field_class = self.serializer_choice_field
# Some model fields may introduce kwargs that would not be valid
# for the choice field. We need to strip these out.
# Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
valid_kwargs = set((
'read_only', 'write_only',
'required', 'default', 'initial', 'source',
'label', 'help_text', 'style',
'error_messages', 'validators', 'allow_null', 'allow_blank',
'choices'
))
for key in list(field_kwargs.keys()):
if key not in valid_kwargs:
field_kwargs.pop(key)
if 'regex' in field_kwargs:
field_class = drf_fields.RegexField
if not issubclass(field_class, drfm_fields.DocumentField):
# `model_field` is only valid for the fallback case of
# `ModelField`, which is used when no other typed field
# matched to the model field.
field_kwargs.pop('model_field', None)
if not issubclass(field_class, drf_fields.CharField) and not issubclass(field_class, drf_fields.ChoiceField):
# `allow_blank` is only valid for textual fields.
field_kwargs.pop('allow_blank', None)
if field_class is drf_fields.BooleanField and field_kwargs.get('allow_null', False):
field_kwargs.pop('allow_null', None)
field_kwargs.pop('default', None)
field_class = drf_fields.NullBooleanField
return field_class, field_kwargs