Flask-Restless dump_to主键字段

时间:2017-08-10 00:20:54

标签: python flask flask-sqlalchemy flask-restless marshmallow

我遇到的问题可能是bug,但想要与社区进行验证。我基本上试图遵循camelcase传输数据,然后强调数据库。

然而,在person_serializer上,flask-restless将不允许出站" idPerson"由于dump_to =" 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'

0 个答案:

没有答案
相关问题