如何使用数据类、Sqlalchemy 和 marshmallow_sqlalchemy 处理 __init__function
How to handle the __init__function by using dataclass, Sqlalchemy and marshmallow_sqlalchemy
在 SQLAlchemy 的当前稳定版本中,没有将模型映射到数据的方法 class(应该在 v1.4 中可用)。所以我想通过定义来应用我自己的 ORM:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
def __init__(self, name: str):
self.name = name
class UserSchema(SQLAlchemyAutoSchema):
class Meta:
model = User
load_instance = True
user_schema = UserSchema()
我的目标是使用模式从 REST API 加载 json 数据。但是好像自动递增的主键id
是个问题。由于属性 id
仅在数据库中定义,而未在属性列表中定义。所以当我申请user_schema.load(#some_json)
时,我得到了错误报告TypeError: __init__() got an unexpected keyword argument 'id'
跟踪误差是这样的:
@ma.post_load
def make_instance(self, data, **kwargs):
"""Deserialize data to an instance of the model if self.load_instance is True.
Update an existing row if specified in `self.instance` or loaded by primary key(s) in the data;
else create a new row.
:param data: Data to deserialize.
"""
if not self.opts.load_instance:
return data
instance = self.instance or self.get_instance(data)
if instance is not None:
for key, value in data.items():
setattr(instance, key, value)
return instance
kwargs, association_attrs = self._split_model_kwargs_association(data)
> instance = self.opts.model(**kwargs)
E TypeError: __init__() got an unexpected keyword argument 'id'
../../environments/xxx/lib/python3.7/site-packages/marshmallow_sqlalchemy/schema/load_instance_mixin.py:74: TypeError
我的问题是,我应该如何定义模型class,目的是我可以将其用作普通数据class,同时还可以在SQLAlchemy中使用ORM模型?
看来我只需要这样调整架构就可以解决__init__()
问题:
class UserSchema(SQLAlchemyAutoSchema):
class Meta:
model = User
load_instance = True
exclude = ["id"]
在 SQLAlchemy 的当前稳定版本中,没有将模型映射到数据的方法 class(应该在 v1.4 中可用)。所以我想通过定义来应用我自己的 ORM:
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
def __init__(self, name: str):
self.name = name
class UserSchema(SQLAlchemyAutoSchema):
class Meta:
model = User
load_instance = True
user_schema = UserSchema()
我的目标是使用模式从 REST API 加载 json 数据。但是好像自动递增的主键id
是个问题。由于属性 id
仅在数据库中定义,而未在属性列表中定义。所以当我申请user_schema.load(#some_json)
时,我得到了错误报告TypeError: __init__() got an unexpected keyword argument 'id'
跟踪误差是这样的:
@ma.post_load
def make_instance(self, data, **kwargs):
"""Deserialize data to an instance of the model if self.load_instance is True.
Update an existing row if specified in `self.instance` or loaded by primary key(s) in the data;
else create a new row.
:param data: Data to deserialize.
"""
if not self.opts.load_instance:
return data
instance = self.instance or self.get_instance(data)
if instance is not None:
for key, value in data.items():
setattr(instance, key, value)
return instance
kwargs, association_attrs = self._split_model_kwargs_association(data)
> instance = self.opts.model(**kwargs)
E TypeError: __init__() got an unexpected keyword argument 'id'
../../environments/xxx/lib/python3.7/site-packages/marshmallow_sqlalchemy/schema/load_instance_mixin.py:74: TypeError
我的问题是,我应该如何定义模型class,目的是我可以将其用作普通数据class,同时还可以在SQLAlchemy中使用ORM模型?
看来我只需要这样调整架构就可以解决__init__()
问题:
class UserSchema(SQLAlchemyAutoSchema):
class Meta:
model = User
load_instance = True
exclude = ["id"]