Skip to content
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

Custom TYPE_MAPPING #42

Closed
dwieeb opened this issue Nov 19, 2015 · 7 comments

Comments

@dwieeb
Copy link

commented Nov 19, 2015

I am extending the Schema class from marshmallow for my schemas and extending the TYPE_MAPPING attribute, but because of these lines here, the ModelConverter doesn't pick up my changes, because it hardcodes marshmallow's Schema class.

from datetime import datetime
from marshmallow import Schema

[...]

class BaseSchema(Schema):

    TYPE_MAPPING = Schema.TYPE_MAPPING.copy()
    TYPE_MAPPING.update({
        datetime: APIDateTime,
    })

    def __init__(self, *args, **kwargs):
        if 'strict' not in kwargs:
            kwargs['strict'] = True
        super(BaseSchema, self).__init__(*args, **kwargs)

How do I override the mapping in marshmallow-sqlalchemy for datetime => my own custom field?

@fuhrysteve

This comment has been minimized.

Copy link

commented Nov 24, 2015

@dwieeb You're pretty close. I think you're looking for something like this:

from marshmallow_sqlalchemy import ModelSchema, ModelConverter
from sqlalchemy_utils.types import TimezoneType
from marshmallow import fields

class SQLAlchemyUtilsConverter(ModelConverter):
    SQLA_TYPE_MAPPING = dict(
        list(ModelConverter.SQLA_TYPE_MAPPING.items()) +
        [(TimezoneType, fields.Str)]
    )

class MyModelSchema(ModelSchema):
    class Meta:
        model_converter = SQLAlchemyUtilsConverter

only in your case, something more like:

class MyConverter(ModelConverter):
    SQLA_TYPE_MAPPING = dict(
        list(ModelConverter.SQLA_TYPE_MAPPING.items()) +
        [(datetime, APIDateTime)]
    )
@dwieeb

This comment has been minimized.

Copy link
Author

commented Nov 24, 2015

Yeah, I know I can override ModelConverter, I just don't want to. Code bases can easily use thousands of schemas. I shouldn't have to specify model_converter = for each one when I have a BaseSchema with a TYPE_MAPPING that should be picked up.

I think marshmallow-sqlalchemy needs a patch such that _get_field_class_for_data_type() is called with a reference to model from fields_for_model() so that it can use the TYPE_MAPPING from the user's schema classes OR fall back to marshmallow.Schema's TYPE_MAPPING.

@fuhrysteve

This comment has been minimized.

Copy link

commented Nov 24, 2015

Hmm, not sure if I am misunderstanding your issue or not, but you could also do something like this if you wanted something resembling a custom TYPE_MAPPING in your BaseSchema:

class BaseSchema(ModelSchema):
    class Meta:
        model_converter = MyDefaultConverter
        # other defaults

class MyModelSchema(BaseSchema):
    class Meta(BaseSchema.Meta):
        # MyModelSchema specific customizations
        pass

The only downside is that you need to remember to have your Meta classes extend BaseSchema.Meta if you want to customize it further.

@dwieeb

This comment has been minimized.

Copy link
Author

commented Nov 24, 2015

Yeah, that'd be fine for my use case.

I still think the built-in ModelConverter should respect the schema class's type mapping, and not be hardcoded to use the marshmallow schema's type mapping--that's not how inheritance should work. 😕

Perhaps I will do a PR to see how the change is received.

@fuhrysteve

This comment has been minimized.

Copy link

commented Nov 24, 2015

Yeah, I'm not sure if that's intentional or not. PR would be a great way to find out haha

sloria added a commit that referenced this issue Dec 13, 2015

Respect a Schema class's TYPE_MAPPING
Allows Schema subclasses to override TYPE_MAPPING. ModelConverter
will respect the TYPE_MAPPINg of the schema class passed to
its constructor

[resolves #42]

sloria added a commit that referenced this issue Dec 13, 2015

Respect a Schema class's TYPE_MAPPING
Allows Schema subclasses to override TYPE_MAPPING. ModelConverter
will respect the TYPE_MAPPINg of the schema class passed to
its constructor

[resolves #42]
@sloria

This comment has been minimized.

Copy link
Member

commented Dec 13, 2015

#49 should address this issue. ModelSchema will now respect a Schema subclass's TYPE_MAPPING.

@dwieeb

This comment has been minimized.

Copy link
Author

commented Dec 13, 2015

Awesome, @sloria! Thank you.

@dwieeb dwieeb closed this Dec 13, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.