Home > Software engineering >  Django Serializers - Datetime with multiple possible formats
Django Serializers - Datetime with multiple possible formats

Time:01-22

I want my Django Serializer to validate the format of a date according to several possible formats. Essentially, these formats are:

  • just the date (YYYY-MM-DD)
  • date hours (YYYY-MM-DD HH)
  • date hours minutes (YYYY-MM-DD HH:MM)
  • date hours minutes seconds (YYYY-MM-DD HH:MM:SS)

I know that the final two are possible together, using the DateTimeField with format= '%Y-%m-%d %H:%M:%S.

I know the first one is possible by default using the DateField.

I assume the second one is possible using the DateTimeField with format= '%Y-%m-%d %H', but I haven't tested.

What I want to know is: is there a straightforward way to combine all these validators together, without having to use custom functions/validators? For example, my first instinct was to provide a list of possible formats to the format parameter on the Date/DateTime fields, but with no success. Is there something I'm missing?

CodePudding user response:

I got my answer from a comment by Abdul Aziz Barkat:

You want to specify the input_formats argument?...

  •  Tags:  
  • Related