Skip to content

Commit b1aea89

Browse files
dyvetimgraham
authored andcommitted
[1.11.x] Fixed #28105 -- Fixed crash in BaseGeometryWidget.get_context() when overriding existing attrs.
Backport of 75aeebe from master
1 parent a2975cb commit b1aea89

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

django/contrib/gis/forms/widgets.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ def get_context(self, name, value, attrs):
6565
if attrs is None:
6666
attrs = {}
6767

68-
context.update(self.build_attrs(self.attrs, dict(
69-
name=name,
70-
module='geodjango_%s' % name.replace('-', '_'), # JS-safe
71-
serialized=self.serialize(value),
72-
geom_type=gdal.OGRGeomType(self.attrs['geom_type']),
73-
STATIC_URL=settings.STATIC_URL,
74-
LANGUAGE_BIDI=translation.get_language_bidi(),
75-
**attrs
76-
)))
68+
build_attrs_kwargs = {
69+
'name': name,
70+
'module': 'geodjango_%s' % name.replace('-', '_'), # JS-safe
71+
'serialized': self.serialize(value),
72+
'geom_type': gdal.OGRGeomType(self.attrs['geom_type']),
73+
'STATIC_URL': settings.STATIC_URL,
74+
'LANGUAGE_BIDI': translation.get_language_bidi(),
75+
}
76+
build_attrs_kwargs.update(attrs)
77+
context.update(self.build_attrs(self.attrs, build_attrs_kwargs))
7778
return context
7879

7980

docs/releases/1.11.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ Bugfixes
6666

6767
* Updated the ``contrib.postgres`` ``SplitArrayWidget`` to use template-based
6868
widget rendering (:ticket:`28040`).
69+
70+
* Fixed crash in ``BaseGeometryWidget.get_context()`` when overriding existing
71+
``attrs`` (:ticket:`28105`).

tests/gis_tests/test_geoforms.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22

33
from django.contrib.gis import forms
4+
from django.contrib.gis.forms import BaseGeometryWidget
45
from django.contrib.gis.geos import GEOSGeometry
56
from django.forms import ValidationError
67
from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
@@ -353,6 +354,12 @@ class PointForm(forms.Form):
353354
@skipUnlessDBFeature("gis_enabled")
354355
class GeometryWidgetTests(SimpleTestCase):
355356

357+
def test_get_context_attrs(self):
358+
"""The Widget.get_context() attrs argument overrides self.attrs."""
359+
widget = BaseGeometryWidget(attrs={'geom_type': 'POINT'})
360+
context = widget.get_context('point', None, attrs={'geom_type': 'POINT2'})
361+
self.assertEqual(context['geom_type'], 'POINT2')
362+
356363
def test_subwidgets(self):
357364
widget = forms.BaseGeometryWidget()
358365
self.assertEqual(

0 commit comments

Comments
 (0)