RFC: A more concise API for overriding field arguments and consolidating TableSchema and ModelSchema
See original GitHub issueStatus quo
Users often need to provide overrides to autogenerated fields. There are currently two current mechanisms for this.
You can use field_for
. This gets verbose when you use it for many fields.
class ArtistSchema(ModelSchema):
class Meta:
model = models.Artist
created_at = field_for(models.Artist, "created_at", dump_only=True)
# ...
You can also pass overrides on the model.
class Artist(Model):
created_at = Column(Timestamp, info={"marshmallow": {"dump_only": True})
This awkwardly mixes concerns and can be problematic when you need multiple schemas for the same model.
Proposed changes
- Deprecate
ModelSchema
andTableSchema
in favor of aSQLAlchemyAutoSchema
that can take eithermodel
ortable
as an option. LikeModelSchema
andTableSchema
,SQLAlchemyAutoSchema
will autogenerate fields for all columns. Field configuration can be overridden using a newauto_field
function.
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
class ArtistSchema(SQLAlchemyAutoSchema):
class Meta:
model = models.Artist
# OR
# table = models.Artist.__table__
id = auto_field(dump_only=True)
created_at = auto_field(dump_only=True)
- Add a
SQLAlchemySchema
class that associates the schema with a model/table so thatauto_field
can be used. UnlikeSQLAlchemyAutoSchema
, fields won’t be automatically generated for all columns, so fields must be declared explicitly.
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
class ArtistSchema(SQLAlchemySchema):
class Meta:
model = models.Artist
# OR
# table = models.Artist.__table__
id = auto_field(dump_only=True)
created_at = auto_field(dump_only=True)
name = auto_field()
- Deprecate support for
info={"marshmallow": {...}}
, as it is redundant.
h/t @taion for the SQLAlchemySchema/auto_field idea and its implementation (currently outside this repo)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:15 (11 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We have an implementation of this at work in a closed-source repo. It will take a bit of finagling to get it ready for upstream, but hopefully we’ll get to it…soon (sorry I can’t give an accurate estimate).
Not a huge advantage. My rationale is that
SQLAlchemySchema
andSQLAlchemyAutoSchema
behave differently with regards to how they construct the schema at class declaration time. This is unlike the Meta options, e.g.ordered
andrender_module
, which change the behavior of instances. This is a low-confidence opinion.