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

Error dumping objects with JSONB fields (with already dumped date/datetime data) #24

Open
mahenzon opened this issue Oct 11, 2020 · 5 comments
Labels
bug Something isn't working enhancement New feature or request hacktoberfest JSONB

Comments

@mahenzon
Copy link
Collaborator

If you have a model with JSONB field, and there's a dumped date / datetime, marshmallow won't be able to serialize it due to it being serialized already (date and datetime strings will cause an error when tried to serialize)
smth like TypeError: descriptor 'isoformat' requires a 'datetime.date' object but received a 'str'

possible solution: load all JSONB fields when loading object, or just before serializing

from marshmallow import Schema, fields
from sqlalchemy.dialects.postgresql import JSONB


def load_jsonb_columns(instance, schema: Schema) -> None:
    """
    :param instance:
    :param schema:
    :return:
    """
    for attr in type(instance)._sa_class_manager.attributes:
        if not isinstance(attr.expression.type, JSONB):
            continue

        if hasattr(instance, attr.name) and attr.name in schema.declared_fields:
            fld: fields.Nested = schema.declared_fields[attr.name]
            if not isinstance(fld, fields.Nested):
                continue
            deserialized = fld.schema.load(getattr(instance, attr.name))
            setattr(instance, attr.name, deserialized)

also there can be other types which require to be converted

@mahenzon mahenzon added bug Something isn't working enhancement New feature or request hacktoberfest JSONB labels Oct 11, 2020
@rexdivakar
Copy link

Assign it to me i will work on it @mahenzon

@mahenzon
Copy link
Collaborator Author

@rexdivakar cool! Good luck
Please don't forget to provide tests 🙂

@mahenzon
Copy link
Collaborator Author

plz note that modifying object in-place may (and will) cause an error when session is flushed, because JSONB attrs will contain unserializable values. Please consider not modifying real object implicitly

@mahenzon
Copy link
Collaborator Author

@rexdivakar any news?

@rexdivakar
Copy link

Hey @mahenzon Its been a while and I missed this issue, will work on it and raise a PR soon

@rexdivakar rexdivakar removed their assignment Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request hacktoberfest JSONB
Projects
None yet
Development

No branches or pull requests

2 participants