本文整理汇总了Python中marshmallow.EXCLUDE属性的典型用法代码示例。如果您正苦于以下问题:Python marshmallow.EXCLUDE属性的具体用法?Python marshmallow.EXCLUDE怎么用?Python marshmallow.EXCLUDE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类marshmallow
的用法示例。
在下文中一共展示了marshmallow.EXCLUDE属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: deserialize
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def deserialize(cls, obj):
"""
Convert from JSON representation to a model instance.
Args:
obj: The dict to load into a model instance
Returns:
A model instance for this data
"""
schema = cls._get_schema_class()(unknown=EXCLUDE)
try:
return schema.loads(obj) if isinstance(obj, str) else schema.load(obj)
except ValidationError as e:
LOGGER.exception(f"{cls.__name__} message validation error:")
raise BaseModelError(f"{cls.__name__} schema validation failed") from e
示例2: serialize
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def serialize(self, as_string=False) -> dict:
"""
Create a JSON-compatible dict representation of the model instance.
Args:
as_string: Return a string of JSON instead of a dict
Returns:
A dict representation of this model, or a JSON string if as_string is True
"""
schema = self.Schema(unknown=EXCLUDE)
try:
return schema.dumps(self) if as_string else schema.dump(self)
except ValidationError as e:
LOGGER.exception(f"{self.__class__.__name__} message serialization error:")
raise BaseModelError(
f"{self.__class__.__name__} schema validation failed"
) from e
示例3: test_unknown_fields
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def test_unknown_fields(self):
class ExcludeBaseSchema(ma.Schema):
class Meta:
unknown = ma.EXCLUDE
@self.instance.register
class ExcludeUser(self.User):
MA_BASE_SCHEMA_CLS = ExcludeBaseSchema
user_ma_schema_cls = self.User.schema.as_marshmallow_schema()
assert issubclass(user_ma_schema_cls, ma.Schema)
exclude_user_ma_schema_cls = ExcludeUser.schema.as_marshmallow_schema()
assert issubclass(exclude_user_ma_schema_cls, ExcludeBaseSchema)
data = {'name': 'John', 'dummy': 'dummy'}
excl_data = {'name': 'John'}
# By default, marshmallow schemas raise on unknown fields
with pytest.raises(ma.ValidationError) as excinfo:
user_ma_schema_cls().load(data)
assert excinfo.value.messages == {'dummy': ['Unknown field.']}
# With custom schema, exclude unknown fields
assert exclude_user_ma_schema_cls().load(data) == excl_data
示例4: test_post_resource
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def test_post_resource(endpoint, admin_token, session, client, app):
"""
POST /resources.
"""
resource = resource_from_endpoint(app, endpoint)
# Construct an instance of the model
model = resource.data_layer["model"]
factory = find_factory(model)
instance = factory()
session.commit()
clone = factory_clone(instance, factory)
# clone = clone_model(instance)
session.expunge(clone)
# If we're pretending to be a client, we don't want to send the dump_only fields
# that might be computed
dump_only = dump_only_fields(resource.schema)
request = resource.schema(many=False, use_links=False, exclude=dump_only).dump(
clone
)
count_1 = session.query(model).count()
# Do the request
url = url_for(endpoint)
rv = client.post(url, json=request, headers={"access_token": admin_token})
# Check the request was successful
assert rv.status_code == 201, rv.json["errors"]
ret = rv.json
del ret["jsonapi"]
# Check that we now have data
count_2 = session.query(model).count()
assert count_2 - count_1 == 1
# Validate the returned data
data = resource.schema(many=False, unknown=EXCLUDE).load(ret)
示例5: _pagination_parameters_schema_factory
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def _pagination_parameters_schema_factory(
def_page, def_page_size, def_max_page_size):
"""Generate a PaginationParametersSchema"""
class PaginationParametersSchema(ma.Schema):
"""Deserializes pagination params into PaginationParameters"""
class Meta:
ordered = True
if MARSHMALLOW_VERSION_MAJOR < 3:
strict = True
else:
unknown = ma.EXCLUDE
page = ma.fields.Integer(
missing=def_page,
validate=ma.validate.Range(min=1)
)
page_size = ma.fields.Integer(
missing=def_page_size,
validate=ma.validate.Range(min=1, max=def_max_page_size)
)
@ma.post_load
def make_paginator(self, data, **kwargs):
return PaginationParameters(**data)
return PaginationParametersSchema
示例6: schemas
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def schemas():
class DocSchema(CounterSchema):
if MARSHMALLOW_VERSION_MAJOR < 3:
class Meta:
strict = True
item_id = ma.fields.Int(dump_only=True)
field = ma.fields.Int(attribute='db_field')
class DocEtagSchema(CounterSchema):
if MARSHMALLOW_VERSION_MAJOR < 3:
class Meta:
strict = True
field = ma.fields.Int(attribute='db_field')
class QueryArgsSchema(ma.Schema):
class Meta:
ordered = True
if MARSHMALLOW_VERSION_MAJOR < 3:
strict = True
else:
unknown = ma.EXCLUDE
arg1 = ma.fields.String()
arg2 = ma.fields.Integer()
return namedtuple(
'Model', ('DocSchema', 'DocEtagSchema', 'QueryArgsSchema'))(
DocSchema, DocEtagSchema, QueryArgsSchema)
示例7: validate
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def validate(self):
"""Validate a constructed model."""
schema = self.Schema(unknown=EXCLUDE)
errors = schema.validate(self.serialize())
if errors:
raise ValidationError(errors)
return self
示例8: project_clone_view
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def project_clone_view(user_data):
"""Clone a remote repository."""
project_data = ProjectCloneContext().load({
**user_data,
**request.json
},
unknown=EXCLUDE)
project = _project_clone(user_data, project_data)
return result_response(ProjectCloneResponseRPC(), project)
示例9: read_manifest_from_template
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def read_manifest_from_template(user, cache):
"""Read templates from the manifest file of a template repository."""
project_data = ManifestTemplatesRequest().load({
**user,
**request.args,
},
unknown=EXCLUDE)
project = _project_clone(user, project_data)
manifest = read_template_manifest(project.abs_path)
return result_response(
ManifestTemplatesResponseRPC(), {'templates': manifest}
)
示例10: make_project
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def make_project(self, user, project_data):
"""Store user project metadata."""
project_data.update({'user_id': user.user_id})
project_obj = self.project_schema.load(project_data, unknown=EXCLUDE)
project_obj.save()
return project_obj
示例11: exclude_unknown_fields
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def exclude_unknown_fields(schema):
schema.unknown = marshmallow.EXCLUDE
return schema
示例12: publish
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def publish():
result = dataset_metadata.load(request.json, unknown=EXCLUDE)
if dataset_exists(result):
return jsonify('Dataset already exists'), 409
datasets_metadata.insert_one(result)
del result['_id']
return jsonify(result), 201
示例13: test_custom_ma_base_schema_cls
# 需要导入模块: import marshmallow [as 别名]
# 或者: from marshmallow import EXCLUDE [as 别名]
def test_custom_ma_base_schema_cls(self):
# Define custom marshmallow schema base class
class ExcludeBaseSchema(ma.Schema):
class Meta:
unknown = ma.EXCLUDE
# Typically, we'll use it in all our schemas, so let's define base
# Document and EmbeddedDocument classes using this base schema class
@self.instance.register
class MyDocument(Document):
MA_BASE_SCHEMA_CLS = ExcludeBaseSchema
@self.instance.register
class MyEmbeddedDocument(EmbeddedDocument):
MA_BASE_SCHEMA_CLS = ExcludeBaseSchema
# Now, all our objects will generate "exclude" marshmallow schemas
@self.instance.register
class Accessory(MyEmbeddedDocument):
brief = fields.StrField()
value = fields.IntField()
@self.instance.register
class Bag(MyDocument):
item = fields.EmbeddedField(Accessory)
content = fields.ListField(fields.EmbeddedField(Accessory))
data = {
'item': {'brief': 'sportbag', 'value': 100, 'name': 'Unknown'},
'content': [
{'brief': 'cellphone', 'value': 500, 'name': 'Unknown'},
{'brief': 'lighter', 'value': 2, 'name': 'Unknown'}
],
'name': 'Unknown',
}
excl_data = {
'item': {'brief': 'sportbag', 'value': 100},
'content': [
{'brief': 'cellphone', 'value': 500},
{'brief': 'lighter', 'value': 2}]
}
ma_schema = Bag.schema.as_marshmallow_schema()
assert ma_schema().load(data) == excl_data