From the course: Building RESTful Web APIs with Django

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Date and time fields with serializers

Date and time fields with serializers

- [Narrator] Now we want to be able to set the sales dates for our product through the REST API. We are going to use the DateTime field to set the start and end dates of a sale. A Date/Time field can be configured with its input format, the output format, the help text, and style. Let's override the sales start and sale end fields in the Product Serializer. So the sale start is a DateTimeField, and we're setting the input formats. I'm setting the format to None, and we will allow null, it's not a required field, and we are also setting up the help text with the accepted format, and the style that will render this field with a nice placeholder and then we'd do the same for the sale end field. The output format is set to none, so that the sale start date and end date are always DateTime objects. The input formats list can contain any custom format that is accepted by Python's string to time conversion function.…

Contents