question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API to customize the list of TYPE_MAPPING

See original GitHub issue

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 init https://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:

ModelConverter.SQLA_TYPE_MAPPING.update({
    ChoiceType: ChoiceTypeSchema
})

PS: I use flask-marshmallow in my app.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

8reactions
ThiefMastercommented, Oct 7, 2016

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.

3reactions
sloriacommented, Feb 10, 2020

I’m considering making the converter class a class member of the schema rather than class Meta option, i.e.

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customization - OpenAPI Generator
Your api.mustache will be used if it exists in your custom template directory ... This will create a new directory out/generators/my-codegen ...
Read more >
[JAVA] Better type mapping support · Issue #7589 - GitHub
Description My particular frustration is with the mapping of date types. I'm totally on board with format: date mapping to java.time.
Read more >
Fluent API - Configuring and Mapping Properties and Types
This article is designed to demonstrate how to use the fluent API to configure properties. The code first fluent API is most commonly ......
Read more >
Mapping Lists with ModelMapper - Baeldung
Learn how to map lists of different element types using Model Mapper. ... generic type mapping, and property mapping to create object list...
Read more >
Update mapping API | Elasticsearch Guide [8.5] | Elastic
For new fields, this mapping can include: Field name; Field data type · Mapping parameters. For existing fields, see Change the mapping of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found