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

Method to_python

django/forms/fields.py:335–350  ·  view source on GitHub ↗

Validate that int() can be called on the input. Return the result of int() or None for empty values.

(self, value)

Source from the content-addressed store, hash-verified

333 )
334
335 def to_python(self, value):
336 """
337 Validate that int() can be called on the input. Return the result
338 of int() or None for empty values.
339 """
340 value = super().to_python(value)
341 if value in self.empty_values:
342 return None
343 if self.localize:
344 value = formats.sanitize_separators(value)
345 # Strip trailing decimal and zeros.
346 try:
347 value = int(self.re_decimal.sub("", str(value)))
348 except (ValueError, TypeError):
349 raise ValidationError(self.error_messages["invalid"], code="invalid")
350 return value
351
352 def widget_attrs(self, widget):
353 attrs = super().widget_attrs(widget)

Callers

nothing calls this directly

Calls 2

ValidationErrorClass · 0.90
to_pythonMethod · 0.45

Tested by

no test coverage detected