|  | 
| 22 | 22 | from django.utils.http import parse_header_parameters | 
| 23 | 23 | from django.utils.safestring import SafeString | 
| 24 | 24 | 
 | 
| 25 |  | -from rest_framework import VERSION, exceptions, serializers, status | 
|  | 25 | +from rest_framework import ISO_8601, VERSION, exceptions, serializers, status | 
| 26 | 26 | from rest_framework.compat import ( | 
| 27 | 27 |     INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi, coreschema, | 
| 28 | 28 |     pygments_css, yaml | 
| @@ -343,11 +343,23 @@ def render_field(self, field, parent_style): | 
| 343 | 343 |         field = field.as_form_field() | 
| 344 | 344 | 
 | 
| 345 | 345 |         if style.get('input_type') == 'datetime-local': | 
|  | 346 | +            # Because DateTimeField.to_representation returns a string, | 
|  | 347 | +            # need to parse it back to datetime for proper formatting | 
|  | 348 | +            has_format = hasattr(field._field, "format") | 
|  | 349 | + | 
|  | 350 | +            if has_format: | 
|  | 351 | +                if field._field.format is None: | 
|  | 352 | +                    datetime_field_value = field.value | 
|  | 353 | +                else: | 
|  | 354 | +                    datetime_field_value = datetime.datetime.strptime(field.value, field._field.format) | 
|  | 355 | +            else: | 
|  | 356 | +                if api_settings.DATETIME_FORMAT == ISO_8601: | 
|  | 357 | +                    datetime_field_value = datetime.datetime.fromisoformat(field.value.rstrip('Z')) | 
|  | 358 | + | 
| 346 | 359 |             # The format of an input type="datetime-local" is "yyyy-MM-ddThh:mm" | 
| 347 | 360 |             # followed by optional ":ss" or ":ss.SSS", so keep only the first three | 
| 348 | 361 |             # digits of milliseconds to avoid browser console error. | 
| 349 |  | -            datetime_value = field._field.parent.validated_data.get(field.field_name) | 
| 350 |  | -            field.value = datetime_value.replace(tzinfo=None).isoformat(timespec="milliseconds").rstrip('Z') | 
|  | 362 | +            field.value = datetime_field_value.replace(tzinfo=None).isoformat(timespec="milliseconds") | 
| 351 | 363 | 
 | 
| 352 | 364 |         if 'template' in style: | 
| 353 | 365 |             template_name = style['template'] | 
|  | 
0 commit comments