Загрузка зефира JSON TypeError: unhashable тип: 'dict' - PullRequest
0 голосов
/ 14 декабря 2018

Привет, ребята. У меня проблема с загрузкой моего JSON в объект, используя зефир, sqlalchemy и flask.

У меня есть 3 таблицы.Person Student и Modality, Student имеет fk от Person, а Modality имеет fk от Student.

Проблема в том, что я могу создать нового персонажа и модальность, используя схему студента, но я не могу создать новую модальность, используяСхема человека, я могу создать только студент.когда я пытаюсь создать модальность этого человека, я получаю сообщение об ошибке TypeError: unhashable type: '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)

Я получаю сообщение об ошибке с posts_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'
...