11from __future__ import unicode_literals
22
3+ import warnings
4+
35from django import forms
46from django .utils import six
57from django .utils .translation import ugettext_lazy as _
@@ -18,7 +20,7 @@ class GeometryField(forms.Field):
1820 widget = forms .Textarea
1921
2022 default_error_messages = {
21- 'no_geom ' : _ ('No geometry value provided.' ),
23+ 'required ' : _ ('No geometry value provided.' ),
2224 'invalid_geom' : _ ('Invalid geometry value.' ),
2325 'invalid_geom_type' : _ ('Invalid geometry type.' ),
2426 'transform_error' : _ ('An error occurred when transforming the geometry '
@@ -30,13 +32,18 @@ def __init__(self, **kwargs):
3032 # defaults (e.g., allow None).
3133 self .srid = kwargs .pop ('srid' , None )
3234 self .geom_type = kwargs .pop ('geom_type' , 'GEOMETRY' )
33- self .null = kwargs .pop ('null' , True )
35+ if 'null' in kwargs :
36+ kwargs .pop ('null' , True )
37+ warnings .warn ("Passing 'null' keyword argument to GeometryField is deprecated." ,
38+ DeprecationWarning , stacklevel = 2 )
3439 super (GeometryField , self ).__init__ (** kwargs )
3540
3641 def to_python (self , value ):
3742 """
3843 Transforms the value to a Geometry object.
3944 """
45+ if value in self .empty_values :
46+ return None
4047 try :
4148 return GEOSGeometry (value )
4249 except (GEOSException , ValueError , TypeError ):
@@ -48,15 +55,9 @@ def clean(self, value):
4855 object (which is returned). A ValidationError is raised if
4956 the value cannot be instantiated as a Geometry.
5057 """
51- if not value :
52- if self .null and not self .required :
53- # The geometry column allows NULL and is not required.
54- return None
55- else :
56- raise forms .ValidationError (self .error_messages ['no_geom' ])
57-
58- # Transform the value to a python object first
59- geom = self .to_python (value )
58+ geom = super (GeometryField , self ).clean (value )
59+ if geom is None :
60+ return geom
6061
6162 # Ensuring that the geometry is of the correct type (indicated
6263 # using the OGC string label).
0 commit comments