MCPcopy
hub / github.com/django/django / to_python

Method to_python

django/forms/fields.py:544–562  ·  view source on GitHub ↗

Validate that the input can be converted to a datetime. Return a Python datetime.datetime object.

(self, value)

Source from the content-addressed store, hash-verified

542 return value
543
544 def to_python(self, value):
545 """
546 Validate that the input can be converted to a datetime. Return a
547 Python datetime.datetime object.
548 """
549 if value in self.empty_values:
550 return None
551 if isinstance(value, datetime.datetime):
552 return from_current_timezone(value)
553 if isinstance(value, datetime.date):
554 result = datetime.datetime(value.year, value.month, value.day)
555 return from_current_timezone(result)
556 try:
557 result = parse_datetime(value.strip())
558 except ValueError:
559 raise ValidationError(self.error_messages["invalid"], code="invalid")
560 if not result:
561 result = super().to_python(value)
562 return from_current_timezone(result)
563
564 def strptime(self, value, format):
565 return datetime.datetime.strptime(value, format)

Calls 4

from_current_timezoneFunction · 0.90
parse_datetimeFunction · 0.90
ValidationErrorClass · 0.90
to_pythonMethod · 0.45