Skip to content
Advertisement

Elegant Way to Deal with Marshmallow Altering a SQLAlchemy Object

I have found myself in a situation that does not seem to have an elegant solution. Consider the following (pseudo-ish) REST API code

JavaScript

I have

  • A PUT endpoint that will create a new resource, then return the dump of that resource.
  • An ORM object that has a filePath property. This must be stored as a relative path.
  • A Marshmallow schema. It has a @pre_dump method to resolve the file path by the use of another property (parent.rootDir)

So basically the process is

  1. Create the new resource
  2. Create a schema dump of that resource to use
  3. Commit
  4. Return the schema dump

So finally, the problem is: outputDump‘s @pre_dump actually alters ormObject, so that it is now a fully resolved path by the time db.session.commit() is called. My first instinct here was to create a deep copy of ormObject but that fails with

JavaScript

It’s not that this is a difficult thing to solve, but it seems to be difficult to solve elegantly with my current knowledge. I need the path to be relative for the database, and resolved otherwise. My current solution is to tell the SomeOutputSchema to skip the @pre_dump in this case, then take the outputDump and then resolve the file paths just after the schema dump. But this feels really gross to me.

I would love to hear any ideas on this, as currently my code feels messy and I don’t like the idea of just leaving it and pushing on.

Advertisement

Answer

Solved by using a @post_dump and using pass_original=True to get access to the original object

JavaScript
Advertisement