我正在Flask中开发api,使用Marshmallow进行序列化/反序列化/验证,使用SQLAlchemy作为我的ORM.
在我的更新功能中,我想限制可以更新的字段,例如我不希望用户此刻能够更改他们的电子邮件.
为了实现这一点,我设置了一个模式(UserSchema),其字段受元组(UserSchemaTypes.UPDATE_FIELDS)限制.元组不包含电子邮件.
我遇到的问题是电子邮件是我的数据库中用户行的必填字段.
因此,当我使用模式(users_schema.load(user_json))创建User模型对象时,会向sqlalchemy会话添加非法对象.
#schema to validate the posted fields against
users_schema = UserSchema(only=UserSchemaTypes.UPDATE_FIELDS)
#attempt to deserialize the posted json to a User model object using the schema
user_data = users_schema.load(user_json)
if not user_data.errors:#update data passed validation
user_update_obj = user_data.data
User.update(user_id,vars(user_update_obj))
在我的更新函数本身,然后我必须通过db.session.expunge_all()从会话中删除这个非法对象,就好像我没有收到OperationalError.
@staticmethod
def update(p_id,data):
db.session.expunge_all()#hack I want to remove
user = User.query.get(p_id)
for k, v in data.iteritems():
setattr(user, k, v)
db.session.commit()
删除db.session.expunge_all()时收到的OperationalError:
OperationalError: (raised as a result of Query-invoked autoflush; consider
using a session.no_autoflush block if this flush is occurring prematurely)
(_mysql_exceptions.OperationalError) (1048, "Column 'email' cannot be null") [SQL: u'INSERT INTO user (email, password, active, phone, current_login_at, last_login_at, current_login_ip, last_login_ip, login_count) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)'] [parameters: (None, None, 1, '0444', None, None, None, None, None)]
这样做有更好/更清洁的方法吗?
最佳答案 来自Steven Loria
https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/33#issuecomment-147008000
您可以采取以下几种不同的方法:
选项1:字段参数
from marshmallow import Schema, fields, pre_load
class BaseSchema(Schema):
@pre_load
def check_update_fields(self, data)
non_update_fields = set([
fname, fobj for fname, obj in self.fields
if fobj.metadata.get('can_update') is False
])
return {
key: value for key, value in data.items()
if key not in non_update_fields
}
class UserSchema(BaseSchema):
name = fields.Str()
email = fields.Str(can_update=False)
选项2:类Meta选项
from marshmallow import Schema, SchemaOpts, fields, pre_load
class BaseSchemaOpts(SchemaOpts):
def __init__(self, meta):
super().__init__(meta)
self.update_fields = getattr(meta, 'update_fields', set())
class BaseSchema(Schema):
OPTIONS_CLASS = BaseSchemaOpts
email = fields.Str(can_update=False)
@pre_load
def check_update_fields(self, data)
non_update_fields = set(self.fields) - set(self.opts.update_fields)
return {
key: value for key, value in data.items()
if key not in non_update_fields
}
class UserSchema(BaseSchema):
name = fields.Str()
email = fields.Str()
class Meta:
update_fields = ('name', )