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

Problem with loading datetime in Marshmallow Schema. #87

Open
aderbas opened this issue Jun 4, 2024 · 3 comments
Open

Problem with loading datetime in Marshmallow Schema. #87

aderbas opened this issue Jun 4, 2024 · 3 comments
Labels
question Further information is requested

Comments

@aderbas
Copy link

aderbas commented Jun 4, 2024

I'm trying to load the parameters coming from the PUT request into the schema.

# schema.py
class ProjectMergeSchema(ma.Schema):
        id = ma.Integer()
        date_time_created = ma.DateTime()
# views.py
@blueprint.route('/', methods=['PUT'])
@response(project_schema)
@body(merge_schema)
def update(args, service: ProjectService):
    """Update."""
    return service.update(merge_schema.load(args))

input payload:

print(args)
# {'id': 14, 'date_time_created': datetime.datetime(2024, 6, 4, 11, 47, 4)}

The problem here is that the date_time_created field in the args object is already converted to datetime and marshmallow gives error:

marshmallow.exceptions.ValidationError: {'date_time_created': ['Not a valid datetime.']}

Is there any way for the date parameter to remain as a string? I'm using request.json to get around the problem but pylint complains about an unused variable (args in the method).

I do not know if i was clear.

@miguelgrinberg
Copy link
Owner

@miguelgrinberg miguelgrinberg added the question Further information is requested label Jun 4, 2024
@aderbas
Copy link
Author

aderbas commented Jun 5, 2024

The problem is in the values ​​that come from the args that are injected into the method def update(args) I didn't want, for each view, to treat date fields.. When it is a date field it is already converted to python datetime. When serializing within the schema, it gives an error.

@miguelgrinberg
Copy link
Owner

The args that the endpoint receives are already parsed. You are trying to parse them again and that is why you get the error. Once again, have a look at my code, I do not have any issues with datetimes in updates, at least I don't think I do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants