Paul Mazzuca
Paul Mazzuca

Reputation: 637

Flask-Restless dump_to of primary key field

I am running into an issue that may be bug, but want to verify it with the community. I am basically trying to conform to camelcase for transporting data, then underscore for the database.

However, on the person_serializer, flask-restless will not allow an outbound "idPerson" as a result of the dump_to="idPerson". For some reason, it checks that the primary key exists and gets a keyError since the actual key is "id_person", not "idPerson".

Any help would be appreciated.

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  

KEY ERROR IS BELOW

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'

Upvotes: 1

Views: 94

Answers (0)

Related Questions