[Django] #30577: feature request: custom rendering for readonly fields in admin

6 views
Skip to first unread message

Django

unread,
Jun 18, 2019, 6:37:20 PM6/18/19
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-----------------------------------------+------------------------
Reporter: David | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
The new view permission is extremely useful, and encourages more use of
the Django Admin tool. It has highlighted a limitation in the rendering of
`readonly_fields` that can be easily addressed. At the moment,
`readonly_fields` (or all fields when the user has only `can_view`) can
only have custom rendering or formatting if they are custom properties or
new fields (created in the ModelAdmin), existing fields can't be changed.
In my use-case I have a number of rich text enhanced `TextField`s, which
when rendered as read-only show up as HTML and can't be marked safe. In
this case creating a custom field in the `ModelAdmin` where the Field can
be output with `mark_safe()` doesn't work as the original field needs to
exist for users with change permissions, the only other way is to include
the field twice which creates UX issues.

If, in ModelAdmin, you could override the formatting/output of a read-only
field it would address this issue. Alternatively, as mentioned in the
comments of #14802, the idea of Fields having a method that is called by
admin to handle the read-only HTML rendering would also do the trick as is
related.

--
Ticket URL: <https://code.djangoproject.com/ticket/30577>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 19, 2019, 4:09:10 AM6/19/19
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-------------------------------+--------------------------------------

Reporter: David | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Carlton Gibson):

Hi David.

This is interesting. I think I see where you're coming from but, could I
ask you to put together an minimal project that demonstrates exactly the
issue you're seeing — perhaps with some screenshots etc — so we can make
sure we're 100% clear?

I'm not sure about an adjustment here. I think it would depend on exactly
what's being proposed. Do you have a specific implementation idea?

It may be that there's a work-around.

bf39978a53f117ca02e9a0c78b76664a41a54745 introduced a check on the widget
for a `read_only` attribute, when rendering `AdminReadonlyField`


{{{
if field in self.form.fields:
widget = self.form[field].field.widget
# This isn't elegant but suffices for contrib.auth's
# ReadOnlyPasswordHashWidget.
if getattr(widget, 'read_only', False):
return widget.render(field, value)
}}}

If you were to override, `ModelAdmin.get_form()` to set a custom widget
(with `read_only=True) on the required field, when the user did not have
the `change` permission, you should 🤔 be able to leverage this to get the
behaviour you need. (If you could put together that test project it would
be easy enough to have a play with this...)

Given these questions, and that #14802 was closed as `wontfix`, I'm going
to close this as `needsinfo` right now. A sample project plus a proposal
is probably needed to progress. With just a sample project, asking on the
DevelopersMailingList might provide some help. Exploring the work-around
would be the shortest route forward.

--
Ticket URL: <https://code.djangoproject.com/ticket/30577#comment:1>

Django

unread,
Jun 19, 2019, 4:09:29 AM6/19/19
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-------------------------------+--------------------------------------
Reporter: David | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


--
Ticket URL: <https://code.djangoproject.com/ticket/30577#comment:2>

Django

unread,
Jun 14, 2021, 2:41:21 PM6/14/21
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-------------------------------+--------------------------------------
Reporter: David | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Florian Demmer):

* cc: Florian Demmer (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30577#comment:3>

Django

unread,
Aug 4, 2021, 11:02:41 AM8/4/21
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-------------------------------+--------------------------------------
Reporter: David | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Filipe):

Just ended up here while looking for a way to have a custom widget on a
read-only JSONField and bumping into `ReadOnlyPasswordHashWidget` hackery
on my own...

Wouldn't it be cleaner (and easy) to simple update
https://github.com/django/django/blob/main/django/contrib/admin/templates/admin/includes/fieldset.html#L16-L18
to check for an existing `field.readonly_widget` all the time?

--
Ticket URL: <https://code.djangoproject.com/ticket/30577#comment:4>

Django

unread,
Nov 4, 2021, 10:12:23 AM11/4/21
to django-...@googlegroups.com
#30577: feature request: custom rendering for readonly fields in admin
-------------------------------+--------------------------------------
Reporter: David | Owner: nobody
Type: New feature | Status: closed
Component: contrib.admin | Version: 2.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlos Palol):

* cc: Carlos Palol (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30577#comment:5>

Reply all
Reply to author
Forward
0 new messages