Skip to content

Commit 28b5378

Browse files
committed
Fix InstanceSelectorBlocks in new wagtail versions
1 parent dadf1a5 commit 28b5378

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Much of this library was built atop of the work of others, specifically:
201201

202202
## Development notes
203203

204+
### Upgrading Wagtail versions
205+
206+
When upgrading this, ensure both `InstanceSelectorPanel` and `InstanceSelectorBlock` are tested manually via the example projects as they use different JavaScript integration approaches.
204207

205208
### Run tests
206209

instance_selector/blocks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django import forms
21
from django.utils.functional import cached_property, lazy
32
from wagtail.blocks import ChooserBlock
43
from wagtail.blocks.field_block import FieldBlockAdapter
@@ -10,8 +9,6 @@
109

1110

1211
class InstanceSelectorBlock(ChooserBlock):
13-
widget = forms.TextInput # Dummy widget to satisfy Wagtail internals
14-
1512
class Meta:
1613
icon = "placeholder"
1714

@@ -29,9 +26,12 @@ def __init__(self, target_model, **kwargs):
2926
def target_model(self):
3027
return resolve_model_string(self._target_model)
3128

29+
@cached_property
30+
def widget(self):
31+
return InstanceSelectorWidget(self.target_model)
32+
3233
def get_form_state(self, value):
33-
widget = InstanceSelectorWidget(self.target_model)
34-
return widget.get_value_data(value)
34+
return self.widget.get_value_data(value)
3535

3636
def get_instance_selector_icon(self):
3737
instance_selector = registry.get_instance_selector(self.target_model)

instance_selector/widgets.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ def get_js_config(self, id_, name):
144144

145145
def build_attrs(self, *args, **kwargs):
146146
attrs = super().build_attrs(*args, **kwargs)
147-
attrs["data-controller"] = "instance-selector"
148-
attrs["data-instance-selector-config-value"] = json.dumps(
149-
self.get_js_config(self.id_, self.name)
150-
)
147+
148+
# Add Stimulus controller attributes for InstanceSelectorPanel usage.
149+
# Skip during telepath serialisation for InstanceSelectorBlock
150+
# (when id_ hasn't been set yet).
151+
if hasattr(self, 'id_') and hasattr(self, 'name'):
152+
attrs["data-controller"] = "instance-selector"
153+
attrs["data-instance-selector-config-value"] = json.dumps(
154+
self.get_js_config(self.id_, self.name)
155+
)
156+
151157
return attrs
152158

153159
@property

tests/test_instance_selector.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
class Tests(WebTest):
2020
"""
21-
Commented out tests are failing for the Wagtail 4.0 + releases.
22-
23-
Im not sure how relevant they are now that the widgets are being rendered
24-
by javascript.
21+
Tests for both InstanceSelectorPanel and InstanceSelectorBlock functionality:
22+
- InstanceSelectorPanel is tested via ModelAdmin edit views.
23+
- InstanceSelectorBlock is tested via StreamField edit views.
2524
"""
2625

2726
def setUp(self):

0 commit comments

Comments
 (0)