我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用marshmallow.fields.Boolean()。
def inline(self, field, context): # type: (fields.Field, JitContext) -> Optional[str] """Generates a template for inlining boolean serialization. For example, generates: ( (value in __some_field_truthy) or (False if value in __some_field_falsy else bool(value)) ) This is somewhat fragile but it tracks what Marshmallow does. """ if is_overridden(field._serialize, fields.Boolean._serialize): return None truthy_symbol = '__{0}_truthy'.format(field.name) falsy_symbol = '__{0}_falsy'.format(field.name) context.namespace[truthy_symbol] = field.truthy context.namespace[falsy_symbol] = field.falsy result = ('(({0} in ' + truthy_symbol + ') or (False if {0} in ' + falsy_symbol + ' else dict()["error"]))') return result + ' if {0} is not None else None'
def schema(): class BasicSchema(Schema): class Meta: ordered = True foo = fields.Integer(attribute='@#') bar = fields.String() raz = fields.Method('raz_') meh = fields.String(load_only=True) blargh = fields.Boolean() def raz_(self, obj): return 'Hello!' return BasicSchema()
def convert_BooleanField(self, field, validate=None, **params): return fields.Boolean(**params)