1

我遇到了一个可能是错误的问题,但想与社区进行验证。我基本上是在尝试遵循驼峰式传输数据,然后为数据库下划线。

但是,在 person_serializer 上,由于 dump_to="idPerson",flask-restless 将不允许出站“idPerson”。出于某种原因,它检查主键是否存在并得到一个 keyError,因为实际的键是“id_person”,而不是“idPerson”。

任何帮助,将不胜感激。

class Person(Base):
    __tablename__ = "person"
    id_person = Column(Integer, primary_key=True)
    first_name = Column(String(50))
    last_name = Column(String(50))


class PersonSchema(Schema):
    id_person = fields.Integer(load_from="idPerson",dump_to="idPerson")
    first_name = fields.String(load_from="firstName", dump_to="firstName")
    last_name = fields.String(load_from="lastName", dump_to="lastName")

@post_load
def make_user(self, data):
    return Person(**data)


person_schema = PersonSchema()

def person_serializer(instance):
    return person_schema.dump(instance).data

def person_deserializer(data):
    return person_schema.load(data).data  

关键错误如下

try:
    # Convert the dictionary representation into an instance of the
    # model.
    instance = self.deserialize(data)
    # Add the created model to the session.
    self.session.add(instance)
    self.session.commit()
    # Get the dictionary representation of the new instance as it
    # appears in the database.
    result = self.serialize(instance)
except self.validation_exceptions as exception:
    return self._handle_validation_exception(exception)
# Determine the value of the primary key for this instance and
# encode URL-encode it (in case it is a Unicode string).
pk_name = self.primary_key or primary_key_name(instance)

> primary_key = result[pk_name]
E       KeyError: 'idPerson'
4

0 回答 0