marshmallow-sqlalchemy icon indicating copy to clipboard operation
marshmallow-sqlalchemy copied to clipboard

get_schema_for_field using field.root can create multilevel nesting issues

Open repole opened this issue 8 years ago • 1 comments

Given a schema hierarchy of album->track->genres, if track.genres is a Related field, tracks.genres.model returns the model of the top level schema (Album) rather than the model of the Related field's parent (Track). This causes an attribute error, as Related.model tries to find the genres attribute on the Album model rather than the Track model.

I can't seem to come up with any justification or advantage to using field.root rather than field.parent in get_schema_for_field. Obviously I can override this problem pretty easily, and am happy to do so, but wanted to try and get some insight as to if there was a specific reason for using field.root.

repole avatar Mar 16 '16 04:03 repole

Thanks for reporting, @repole . We use field.root in order to supported "composed" fields.

cclass MySchema(Schema):
    # parent of Str field is MyContainerField instance; root is MySchema
    foo = MyContainerField(fields.Str())

But I see that this will cause problems for > 1 level of nesting. Not sure if we can support both these use cases cleanly. Will need to give this more thought.

sloria avatar May 30 '16 22:05 sloria