Skip to content

Commit c8a8021

Browse files
committed
[qa] Display map widget for readonly users in Django 6.0
Django 6.0 changed how readonly fields are handled in the admin. Setting read_only=True on a widget no longer renders the widget - instead it shows only plain text values. The recommended approach for Django 6.0+ is to disable the field instead of marking it readonly. This allows the widget to render fully while preventing editing, giving readonly users an interactive but non-editable view of the field (e.g., a functional but non-interactive map). Changes: - Modify ReadOnlyMixin.set_readonly_attribute() to use field.disabled=True - Keep read_only attribute on widget for backwards compatibility - Add explanatory comments about Django 6.0 behavior change - All 58 tests pass including readonly field tests This preserves the intended UX where view-only users can see interactive map widgets while being prevented from making changes.
1 parent d1cc0e1 commit c8a8021

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

django_loci/base/admin.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@
2323

2424

2525
class ReadOnlyMixin:
26-
"""Mixin for forms to handle field widgets for view-only users."""
26+
"""Mixin for forms to handle field widgets for view-only users.
27+
28+
For Django 6.0+ compatibility, this mixin disables fields instead of marking
29+
them as readonly. This allows readonly users to see interactive widgets
30+
(like maps) while preventing editing. The `read_only` attribute on widgets
31+
is also set for backwards compatibility with custom widget templates.
32+
"""
2733

2834
def set_readonly_attribute(self, user, fields):
2935
"""
30-
This method sets the read_only attribute on widget for the fields
31-
which are required to be rendered as it is to view-only users. This is
32-
done as 'AdminReadonlyField' renders the widget if 'read_only' is set on
33-
the field's widget. Also the required field must be present in self.fields
36+
This method disables fields for view-only users while keeping widgets
37+
visible and interactive (read-only in terms of input, but visually rich).
38+
39+
For Django 6.0+, we disable the field instead of using readonly on the
40+
widget, which allows the widget to render fully (e.g., Leaflet map).
41+
The `read_only` attribute is still set on widgets for custom templates
42+
that may check for it.
3443
"""
3544
app_label = self.Meta.model._meta.app_label
3645
model_name = self.Meta.model._meta.model_name
@@ -41,6 +50,9 @@ def set_readonly_attribute(self, user, fields):
4150
):
4251
for field in fields:
4352
if field in self.fields:
53+
# Disable the field to prevent editing
54+
self.fields[field].disabled = True
55+
# Also set read_only for custom widget templates compatibility
4456
setattr(self.fields[field].widget, "read_only", True)
4557
# Return 'True' to allow any further handling for view-only users
4658
return True

0 commit comments

Comments
 (0)