dynamorm icon indicating copy to clipboard operation
dynamorm copied to clipboard

Provide dumps() method

Open krismeister opened this issue 4 years ago • 2 comments

The current docs do not give an example of serialization support for JSON.

I attempted to use Marshmallow's dumps feature

db.MyModel.Schema.dumps(someInstanceOfMyModel)
# throws this error:
TypeError: dumps() missing 1 required positional argument: 'obj' marshmallow

But received an error. I am currently using someInstanceOfMyModel._raw to get the data for JSON output.

Feature Request Add a expose a dumps method to get a dict.

db.MyModel.dumps(someInstanceOfMyModel)

krismeister avatar Mar 06 '20 13:03 krismeister

Add a expose a dumps method to get a dict.

If you only want a dict, you can use the to_dict() method.

Try this: someInstanceOfMyModel.to_dict().

ricky-sb avatar Mar 06 '20 23:03 ricky-sb

The obj value that's missing is the instance. You need to use an instantiated Schema:

db.MyModel.Schema().dumps(someInstanceOfMyModel)

I think that the interface of the dumped data from that will vary depending on whether you have marshmallow 2.x or 3.x installed. There was a breaking api change between the two. My project uses marshmallow 2.x so I'm used to doing this to get the dumped data:

data, errors = db.MyModel.Schema().dumps(someInstanceOfMyModel)
# data will the serialized object, errors will be any errors encountered during serialization

Quidge avatar Apr 28 '20 02:04 Quidge