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

Declarative configuration #21

Closed
dpwrussell opened this issue Aug 31, 2015 · 1 comment

Comments

@dpwrussell
Copy link
Contributor

commented Aug 31, 2015

Another idea that is straight from ColanderAlchemy.

I believe this could be implemented relatively easily, but there are numerous different ways one could go about it, so I think perhaps some discussion is more appropriate than forging ahead with a PR which might turn out not to be the best way at all.

I would be really nice to be able to do something like this:

    id = Column(Integer,
                primary_key=True,
                info={
                    'marshmallow_sqlalchemy': {
                        'dump_only': False
                     }
                })

These would then be processed when constructing the schemas to override the settings. Potentially anything that marshmallow can do could be declared there.

I hacked this in quickly:

    def _add_column_kwargs(self, kwargs, column):
        """Add keyword arguments to kwargs (in-place) based on the passed in
        `Column <sqlalchemy.schema.Column>`.
        """
        if column.nullable:
            kwargs['allow_none'] = True

        if hasattr(column.type, 'enums'):
            kwargs['validate'].append(validate.OneOf(choices=column.type.enums))

        # Add a length validator if a max length is set on the column
        if hasattr(column.type, 'length'):
            kwargs['validate'].append(validate.Length(max=column.type.length))

        if hasattr(column.type, 'scale'):
            kwargs['places'] = getattr(column.type, 'scale', None)

        # Primary keys are dump_only ("read-only")
        if getattr(column, 'primary_key', False):
            kwargs['dump_only'] = True

        if 'marshmallow_sqlalchemy' in column.info:
            settings = column.info['marshmallow_sqlalchemy']

            if 'dump_only' in settings:
                kwargs['dump_only'] = settings['dump_only']

            if 'load_only' in settings:
                kwargs['load_only'] = settings['load_only']

and it worked quite well. It didn't support inheritance though which we definitely should I think. That can be achieved relatively easily as well I think as we have for each property, the list of applicable columns, so we just need to iterate over them in order from super to sub and apply the settings found in info to provide ordinary inheritance override behaviour. I think the same thing could be applied to relationships as well.

Thoughts?

@sloria

This comment has been minimized.

Copy link
Member

commented Nov 5, 2018

This has been added in 0.15.0.

@sloria sloria closed this Nov 5, 2018

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