Skip to content

Error serializing MaxValueValidator.limit_value of type datetime.timedelta  #388

@voyc-jean

Description

@voyc-jean

Describe the bug
Similar to #357, the yaml renderer is unable to serialize a MaxValueValidator.limit_value of type datetime.timedelta.

Using the JSON renderer solves the issue.

To Reproduce
Add a MaxValueValidator to a model field with a limit_value type of datetime.timedelta:

import datetime
from django.db import models

class MyModel(models.Model):
    duration = models.DurationField(
        default=datetime.timedelta(seconds=10),
        validators=[
            MaxValueValidator(
                limit_value=datetime.timedelta(seconds=3600),
                message='Limit message.',
            ),
        ],
    )

Raised exception:

  File "/usr/local/lib/python3.8/site-packages/yaml/representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', datetime.timedelta(seconds=3600))

Expected behavior
The yaml render should be able to serialize datetime.timedelta values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions