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

Method to_python

django/contrib/gis/forms/fields.py:36–61  ·  view source on GitHub ↗

Transform the value to a Geometry object.

(self, value)

Source from the content-addressed store, hash-verified

34 self.widget.attrs["geom_type"] = self.geom_type
35
36 def to_python(self, value):
37 """Transform the value to a Geometry object."""
38 if value in self.empty_values:
39 return None
40
41 if not isinstance(value, GEOSGeometry):
42 if hasattr(self.widget, "deserialize"):
43 value = self.widget.deserialize(value)
44 else:
45 try:
46 value = GEOSGeometry(value)
47 except (GEOSException, ValueError, TypeError):
48 value = None
49 if value is None:
50 raise ValidationError(
51 self.error_messages["invalid_geom"], code="invalid_geom"
52 )
53
54 # Try to set the srid
55 if not value.srid:
56 try:
57 value.srid = self.widget.map_srid
58 except AttributeError:
59 if self.srid:
60 value.srid = self.srid
61 return value
62
63 def clean(self, value):
64 """

Callers 4

has_changedMethod · 0.95
test_geom_typeMethod · 0.95
test_to_pythonMethod · 0.95

Calls 3

GEOSGeometryClass · 0.90
ValidationErrorClass · 0.90
deserializeMethod · 0.45

Tested by 3

test_geom_typeMethod · 0.76
test_to_pythonMethod · 0.76