更新了我的项目中的依赖项
以前
marshmallow-sqlalchemy==0.17.0
marshmallow==3.0.0b6
之后
marshmallow-sqlalchemy==0.21.0
marshmallow==3.5.0
我有这样一个模型(简化):
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyModel(Base):
__tablename__ = 'my_model'
id = Column(BigInteger, primary_key=True)
field_name = Column(String)
password = Column(String)
和模式:
from marshmallow import fields
from marshmallow_sqlalchemy import (
ModelSchema,
field_for,
)
class RegistrarSchema(ModelSchema):
class Meta:
model = MyModel
other_field_name = fields.Function(lambda obj: obj.field_name)
field_name = field_for(MyModel, 'field_name', load_from='other_field_name')
password = fields.Method("get_password")
def get_password(self, obj):
return decrypt_password(obj)
def load(self, data, session=None, instance=None, transient=False, *args, **kwargs):
if 'password' in data:
password = data['password']
instance = encrypt_password(password, instance)
return super(ModelSchema, self).load(
data, *args, session=session, instance=instance,
transient=transient, **kwargs
)
使用这个代码我可以调用
RegistrarSchema.load(data={'other_field_name': 'my_value'}, instance=instance, session=session)
该值将保存到数据库中的字段_name中
更新后,我失去了这个功能-现在我得到
File "myfile.py", line 116, in load
transient=transient, **kwargs
File "/usr/local/lib/python3.7/dist-packages/marshmallow_sqlalchemy/schema.py", line 214, in load
return super().load(data, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/marshmallow/schema.py", line 723, in load
data, many=many, partial=partial, unknown=unknown, postprocess=True
File "/usr/local/lib/python3.7/dist-packages/marshmallow/schema.py", line 904, in _do_load
raise exc
marshmallow.exceptions.ValidationError: {'other_field_name': ['Unknown field.']}
并且在更新密码时出现完全相同的错误-
RegistrarSchema.load(data={'password': 'my_value'}, instance=instance, session=session)
我并没有自己写这段代码,也不能联系开发人员
正如我所理解的,第一种情况的目标是提供双向写的能力-data={'my_field': 'value'}
和data={'other_field_name': 'value'}
。第一个场景仍在新版本上工作,但第二个场景没有
在使用密码的情况下,目标是使加密/解密数据的序列化/反序列化更加复杂
我阅读了更新说明,但找不到与此问题相关的任何信息,有人能帮忙吗
在v3中,棉花糖将默认行为从EXCLUDE更改为RAISEhttps://marshmallow.readthedocs.io/en/stable/quickstart.html#handling-unknown-fields
通过在模式的Meta类中指定unknown,可以恢复到旧行为:
在实例化时
或者在调用load时
load中设置的未知选项值将覆盖实例化时应用的值,该值本身将覆盖在Meta类中定义的值
相关问题 更多 >
编程相关推荐