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

Interface for customizing relationship columns

Open jmcarp opened this issue 9 years ago • 1 comments

Back in 0.1, we had a basic interface for customizing the behavior of relationship fields via a keygetter passed to QuerySelect or QuerySelectList. That wound up being a less than ideal way to model relationships, which is why we introduced the Related field. Still, it would still be helpful to expose some interface for customizing related fields without overriding every Related by hand. For example, users may want to use something like StringRelatedField from DRF or Relationship from marshmallow-jsonapi.

At the moment, it seems like you'd subclass ModelConverter and override some combination of _get_field_class_for_property and _add_relationship_kwargs to do this. Which works, but seems kind of complicated, and requires users to know a lot about private APIs. What about adding options to ModelSchemaOpts for setting the field class for relationships, defaulting to Related? Or adding an optional related_factory that's invoked on converting relationships?

jmcarp avatar Oct 11 '15 04:10 jmcarp

+1 to this. This would make implementing something like HyperlinkModelSchema much easier. A related_field class Meta option was my first instinct. Not sure if a factory would be overkill or not; would need to think about use cases.

sloria avatar Oct 11 '15 04:10 sloria