lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.
https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html
MIT License
456 stars 78 forks source link

Ability to override nested class #266

Closed ddorian closed 3 months ago

ddorian commented 3 months ago

I need to override the nested class. This is to use the same object in different presentations. A simple example would be we have a User class. And we want 2 presentations of it, one as UserDetails (this is the User.Schema by default) on an API endpoint. The other as a UserEvent, which has a different schema altogether.

Is this approach ok? I can add tests etc.

dairiki commented 3 months ago

Maybe I'm misunderstanding your goal here, but marshmallow_field can already be used to specify an explicit schema field for a particular dataclass field.

@dataclass
class Parent:
    user: User = field(metadata={marshmallow_field=marshmallow.field.Nested(UserEventSchema)})
ddorian commented 3 months ago

You're correct! Thank you!