Home > Net >  python django serializer wrong date time format for DateTimeField
python django serializer wrong date time format for DateTimeField

Time:01-26

I'm using Django 3.0.2.

I have a serializer defined:

class ValueNestedSerializer(request_serializer.Serializer):
    lower = request_serializer.DateTimeField(required=True, allow_null=False, format=None, input_formats=['%Y-%m-%dT%H:%M:%SZ',])
    upper = request_serializer.DateTimeField(required=True, allow_null=False, format=None, input_formats=['%Y-%m-%dT%H:%M:%SZ',])

class DateRangeSerializer(request_serializer.Serializer):
    attribute = request_serializer.CharField(default="UPLOAD_TIME")
    operator = request_serializer.CharField(default="between_dates")
    value = ValueNestedSerializer(required=True)
    timezone = request_serializer.CharField(default="UTC")
    timezoneOffset = request_serializer.IntegerField(default=0)

class BaseQueryPayload(request_serializer.Serializer):
    appid = request_serializer.CharField(required=True, validators=[is_valid_appid])
    filters = request_serializer.ListField(
        required=True, validators=[is_validate_filters],
        min_length=1
    )
    date_range = DateRangeSerializer(required=True)

And the payload :

{
    "appid": "6017cef554df4124274ef36d",
    "filters": [
        {
            "table": "session",
            "label": "1month"
        }
    ],
    "date_range": {
        "value": {
            "lower": "2023-01-01T01:00:98Z",
            "upper": "2023-01-20T01:00:98Z"
        }
    },
    "page": 1
}

But I get this validation error:

{
    "error": {
        "date_range": {
            "value": {
                "lower": [
                    "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm:ssZ."
                ],
                "upper": [
                    "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm:ssZ."
                ]
            }
        }
    }
}

The suggested format YYYY-MM-DDThh:mm:ssZ is similar to what is passed.

Am I missing anything here?

CodePudding user response:

Format is wrong because seconds must be in 0-59 range (while you try to post 98s)

  • Related