大家好,我遇到了使用棉花糖,sqlalchemy和flask将JSON加载到对象中的问题。
我有3张桌子。人物学生和情态,学生来自fk人物,形态来自学生。
问题是我可以使用学生模式创建一个新的人和模态,但是我不能使用人模式创建一个新的模态,我只能创建一个学生。当我尝试由人创建模态时,出现错误TypeError:不能哈希的类型:'dict'。
class Person(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
.
.
.
salesperson = db.relationship('Salesperson', uselist=False, backref=db.backref('person_salesperson'), lazy='joined')
student = db.relationship('Student', uselist=False, backref=db.backref('person_student'), lazy='joined')
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
person_id = db.Column(db.Integer, db.ForeignKey('person.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
sales = db.relationship('Sales', backref=db.backref('student_sales'), lazy='joined')
team = db.relationship('Team', backref=db.backref('student_team'), lazy='joined')
class Team(db.Model):
student_id = db.Column(db.Integer, db.ForeignKey(
'student.id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, nullable=False)
modality_id = db.Column(db.Integer, db.ForeignKey(
'modality.id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True, nullable=False)
modality = db.relationship('Modality', backref=db.backref('team_modality'),
lazy='joined')
class PersonSchema(ma.ModelSchema):
class Meta:
model = Person
def register_person():
posted_person = schema.PersonSchema().load(request.get_json(), session=db.session)
我在posted_person中遇到错误,我认为不需要放置其余功能代码。
{
"name":"Pedro",
.
.
.
"student":{
"team":[
{"modality_id":1}
]
}
}
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/magnus/Atletica/ams/ams/people/routes.py", line 28, in register_person
posted_person = schema.PersonSchema().load(request.get_json(), session=db.session)
File "/usr/lib/python3.7/site-packages/marshmallow_sqlalchemy/schema.py", line 194, in load
return super(ModelSchema, self).load(data, *args, **kwargs)
File "/usr/lib/python3.7/site-packages/marshmallow/schema.py", line 571, in load
result, errors = self._do_load(data, many, partial=partial, postprocess=True)
File "/usr/lib/python3.7/site-packages/marshmallow/schema.py", line 653, in _do_load
index_errors=self.opts.index_errors,
File "/usr/lib/python3.7/site-packages/marshmallow/marshalling.py", line 290, in deserialize
index=(index if index_errors else None)
File "/usr/lib/python3.7/site-packages/marshmallow/marshalling.py", line 62, in call_and_store
value = getter_func(data)
File "/usr/lib/python3.7/site-packages/marshmallow/marshalling.py", line 283, in <lambda>
data
File "/usr/lib/python3.7/site-packages/marshmallow/fields.py", line 264, in deserialize
output = self._deserialize(value, attr, data)
File "/usr/lib/python3.7/site-packages/marshmallow_sqlalchemy/fields.py", line 111, in _deserialize
return self.related_model(**value)
File "<string>", line 4, in __init__
File "/usr/lib/python3.7/site-packages/sqlalchemy/orm/state.py", line 424, in _initialize_instance
manager.dispatch.init_failure(self, args, kwargs)
File "/usr/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
raise value
File "/usr/lib/python3.7/site-packages/sqlalchemy/orm/state.py", line 421, in _initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/usr/lib/python3.7/site-packages/sqlalchemy/ext/declarative/base.py", line 748, in _declarative_constructor
setattr(self, k, kwargs[k])
File "/usr/lib/python3.7/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "/usr/lib/python3.7/site-packages/sqlalchemy/orm/dynamic.py", line 152, in set
constants = old_collection.intersection(new_values)
File "/usr/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 612, in intersection
result._members.update(self._working_set(members).intersection(other))
File "/usr/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 423, in intersection
other = set(other)
TypeError: unhashable type: 'dict'