-
Notifications
You must be signed in to change notification settings - Fork 94
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
API to customize the list of TYPE_MAPPING #75
Comments
@stephane why not subclass
but as I map
+1 for a more elegant API for mapping customize. |
Yeah, I think it might make sense to have a I would certainly review and merge a PR implementing this. |
#83 added support for TypeDecorated fields, which are heavily used in SQLAlchemy-Utils, so your code shouldn't fail on start now (if you install Marshmallow-sqlalchemy >=0.12.0-dev version)), so now you can just override the auto-generated field defining it explicitly on your Schema class. |
I don't think it makes sense to specify a custom converter via Meta if you have a custom SQLAlchemy type that's used everywhere in your application. There should be a clean/documented way to globally register a new type. |
Following up on this, it would be great if there were an easy to have |
I think adding adding a For the class Meta:
type_mapping_overrides = {
sqlalchemy_utils.ChoiceType: lambda column, kwargs: EnumField(column.type.choices, **kwargs)
} |
I'm considering making the converter class a class member of the schema rather than class SQLAlchemySchema(...):
Converter = ModelConverter This could simplify overriding converter methods and attributes, incl. the type mapping. class MyBaseSchema(SQLAlchemySchema):
class Converter(SQLAlchemySchema.Converter):
SQLA_TYPE_MAPPING = {
**SQLAlchemySchema.Converter.SQLA_TYPE_MAPPING,
datetime.datetime: UnixTimeStampField,
} Thoughts? |
I'm not sure how I feel about moving the converter off of "meta". It seems to me that it intuitively should be "next to" the declaration of the model or table to which the schema is bound. If those live on the |
The converter isn't something that will typically differ per subclass. I'd expect that it would be customized once at the base Schema level. In that sense, it's conceptually closer to |
Is the only thing this saves in practice not having to use a custom schema opts subclass? I guess I don't see offhand why it should be different from, say, I wonder, though, if it would be better for |
Not exactly. You can still get away without a custom options class, as in the comment above.
Fair point. More precisely, I think the distinction is that The goal is to make two common use cases more ergonomic:
Adding Meta.type_mapping_overrides or putting |
Well, would it be reasonable to do both? Can the converter look at the schema to get the type mapping? It feels more weird for type mapping to work differently in the SQLAlchemy context than for configuring the converter to take a few extra steps, IMO. |
So... as long as the API is not implemented yet what's the simplest way to extend the |
I use sqlalchemy_utils which provide ChoiceType field (and many others).
I'm able to create a new Marshmallow field (Field) to handle this new field with proper
_serialize/_deserialize
functions but instead to override the attributes one by one in each schema I defined I want to update the list type_mapping in ModelConverter.I didn't find how to access it properly, ModelConverter accepts
schema_cls
attribute at inithttps://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/dev/marshmallow_sqlalchemy/convert.py#L76
but I'm not able to figure out how to use it properly with all these meta classes ;)
So for now I rely on a monkey patch:
PS: I use flask-marshmallow in my app.
The text was updated successfully, but these errors were encountered: