DEV Community

Flávia Bastos
Flávia Bastos

Posted on • Originally published at flaviabastos.ca on

4

Django Rest Framework: adding DateTimeField format serializer validation

Here’s an example of adding date and time validation to a DateTimeField in a serializer in the Django Rest Framework (DRF).

A first pass, using only the default formats of DateTimeField(), would look like the following:

class CustomSearchFieldsSerializer(serializers.Serializer):
   start_time = serializers.DateTimeField()
   end_time = serializers.DateTimeField()

   def validate(self, data):
       # extra validation to ensure that the end date is always after the start date if data['start_time'] > data['end_time']:
           raise serializers.ValidationError("finish must occur after start")
       return data
Enter fullscreen mode Exit fullscreen mode

Sending a request to that endpoint with the following query parameters (note it only has a date, not time):

/api/v1/custom-search/?start_time=2022-11-01&end_time=2022-11-07
Enter fullscreen mode Exit fullscreen mode

Returns the following 400 Bad Request:

{
   "start_time": [
       "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
   ],
   "end_time": [
       "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
   ]
}
Enter fullscreen mode Exit fullscreen mode

Not exactly what we want… I want this to be flexible and accept date only as a valid option.

Doing some research on the Django Rest Framework (DRF) documentation, I learned that I could customize the accepted formats. The next iteration looks like this:

class CustomSearchFieldsSerializer(serializers.Serializer):
   start_time = serializers.DateTimeField(required=False,
                                          input_formats=["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"])
   end_time = serializers.DateTimeField(required=False,
                                        input_formats=["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%d"])
Enter fullscreen mode Exit fullscreen mode

The valid input formats can be passed as strings in a list. We add a few options, including a date-only format (the last option in the list).

Note: setting the flag “required” to False: this will make this field optional (in this case, the request could have no start and/or end time, which will then require the setting of some default values in the business logic to limit the query).

For the purposes of the task I was working on, I settled on the following formats:

input_formats=["%Y-%m-%d", "iso-8601"]

Enter fullscreen mode Exit fullscreen mode

The first one accepts date only and the second follows the iso-8601 format, which is also the default format when nothing is set – the exact behaviour that we saw in the very first iteration. iso-8601 is an international standard for the representation of date and time formats.


If you found this helpful, please share this article.

The post Django Rest Framework: adding DateTimeField format serializer validation was originally published at flaviabastos.ca

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (1)

Collapse
 
anilkumar541 profile image
Anil kumar

good article Flavia thanks

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay