Skip to content

Commit 18e990f

Browse files
committed
Fixed django#16110 -- Fixed GeometryField odd behaviour regarding null values
Thanks slinkp for the report and the initial patch.
1 parent a87ff95 commit 18e990f

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

django/contrib/gis/db/models/fields.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ def db_type(self, connection):
202202

203203
def formfield(self, **kwargs):
204204
defaults = {'form_class' : forms.GeometryField,
205-
'null' : self.null,
206205
'geom_type' : self.geom_type,
207206
'srid' : self.srid,
208207
}

django/contrib/gis/forms/fields.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import unicode_literals
22

3+
import warnings
4+
35
from django import forms
46
from django.utils import six
57
from 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).

django/contrib/gis/tests/test_geoforms.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.forms import ValidationError
22
from django.contrib.gis.gdal import HAS_GDAL
33
from django.contrib.gis.tests.utils import HAS_SPATIALREFSYS
4+
from django.utils import six
45
from django.utils import unittest
56

67

@@ -37,15 +38,13 @@ def test02_null(self):
3738
"Testing GeometryField's handling of null (None) geometries."
3839
# Form fields, by default, are required (`required=True`)
3940
fld = forms.GeometryField()
40-
self.assertRaises(forms.ValidationError, fld.clean, None)
41-
42-
# Still not allowed if `null=False`.
43-
fld = forms.GeometryField(required=False, null=False)
44-
self.assertRaises(forms.ValidationError, fld.clean, None)
41+
with six.assertRaisesRegex(self, forms.ValidationError,
42+
"No geometry value provided."):
43+
fld.clean(None)
4544

4645
# This will clean None as a geometry (See #10660).
4746
fld = forms.GeometryField(required=False)
48-
self.assertEqual(None, fld.clean(None))
47+
self.assertIsNone(fld.clean(None))
4948

5049
def test03_geom_type(self):
5150
"Testing GeometryField's handling of different geometry types."

0 commit comments

Comments
 (0)