New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post_dump: where are the raw data ? #216

Closed
gouyou opened this Issue May 18, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@gouyou

gouyou commented May 18, 2015

I've read #153 and I'm missing something, where do we get the raw data?

My specific use case is a bit borderline:
I have an object A which contains a list of object Bs, and each object B has a dictionary. I wanted to do something like:

class ASchema(Schema):
    bs = fields.Nested('BSchema', many=True)

class BSchema(Schema):
    @postdump
    def f(self, data, obj):
        for k, v in obj.extra_data.iteritems():
            data[k] = v
        return data

Quickly reading through the documentation I tried it with data_handler to be running into issues with A containing a collection of B instances. Looking into dump_data, I thought that raw meant I would have access to the raw data, but it just let you know if it's a collection or not. So it doesn't loo like post_dump works for me...
Any other solution for my use case?

@sloria

This comment has been minimized.

Member

sloria commented Aug 24, 2015

One simple solution would be to pass the extra argument:

BSchema(extra=obj.extra_data).dump(obj)

Alternatively, you could add object to the Schema's context so that it is accessible to your post_load handler:

class BSchema(Schema):
    @post_dump
    def add_extra_data(self, data):
        obj = self.context.get('obj', None)
        for key, value in getattr(obj, 'extra_data', {}).items():
            data[key] = value
        return data

thing = Thing()
sch = MySchema()
sch.context['obj'] = thing
sch.dump(thing)

Does this meet your use case?

@sloria sloria added the question label Aug 24, 2015

@sloria

This comment has been minimized.

Member

sloria commented Sep 12, 2015

I am considering adding a pass_object parameter to the pre/post_load/dump decorators that would add the target object to the signature of the methods.

@sloria

This comment has been minimized.

Member

sloria commented Sep 16, 2015

The pass_original parameter has been added to post_dump and post_load in 1463a61

@sloria sloria closed this Sep 16, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment