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

Method to_python

django/db/models/fields/__init__.py:2660–2686  ·  view source on GitHub ↗
(self, value)

Source from the content-addressed store, hash-verified

2658 return "TimeField"
2659
2660 def to_python(self, value):
2661 if value is None:
2662 return None
2663 if isinstance(value, datetime.time):
2664 return value
2665 if isinstance(value, datetime.datetime):
2666 # Not usually a good idea to pass in a datetime here (it loses
2667 # information), but this can be a side-effect of interacting with a
2668 # database backend (e.g. Oracle), so we'll be accommodating.
2669 return value.time()
2670
2671 try:
2672 parsed = parse_time(value)
2673 if parsed is not None:
2674 return parsed
2675 except ValueError:
2676 raise exceptions.ValidationError(
2677 self.error_messages["invalid_time"],
2678 code="invalid_time",
2679 params={"value": value},
2680 )
2681
2682 raise exceptions.ValidationError(
2683 self.error_messages["invalid"],
2684 code="invalid",
2685 params={"value": value},
2686 )
2687
2688 def pre_save(self, model_instance, add):
2689 if self.auto_now or (self.auto_now_add and add):

Callers 1

get_prep_valueMethod · 0.95

Calls 2

parse_timeFunction · 0.90
timeMethod · 0.80

Tested by

no test coverage detected