Opened 9 years ago

Closed 7 years ago

#3990 closed (duplicate)

readonly fields for admin

Reported by: philippe.raoult@… Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords: nfa-someday
Cc: cscott@…, tom@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:


This patch adds a readonly_fields attribute to the ModelAdmin class. Fields included in the list can be set in the add form but not in the change form. They will be displayed as text in the change form. It works as follows:

class TestModel(models.Model):
       dummy = models.CharField(maxlength=10)

class TestAdmin(admin.ModelAdmin):
        readonly_fields = ('dummy',)

This feature is purely admin-side and does not interact with the editable=False option. Fields marked with editable=False will not be displayed at all and they will not be part of the django.newform.forms (ie the behavior of editable is left untouched).

How is it implemented:
If a field is in the readonly list, its widget will be set to DisplayWidget, which only prints the value (ie no <input> tag).

Currently this might not work for all type of fields because I'm awaiting confirmation on the design and API. Once the devs are ok, I will extend the patch to work with all fields.

note: this ticket is similar to #1714 in functionality but only modifies the admin side. Forms are not patched.

Attachments (3)

admin_readonly_fields_v1.patch (3.6 KB) - added by philippe.raoult 9 years ago.
admin_readonly_fields_v1.diff (4.3 KB) - added by philippe.raoult@… 9 years ago.
admin_readonly_fields_v1.1.diff (7.5 KB) - added by philippe.raoult@… 9 years ago.

Download all attachments as: .zip

Change History (14)

Changed 9 years ago by philippe.raoult

Changed 9 years ago by philippe.raoult@…

comment:1 Changed 9 years ago by philippe.raoult@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

sorry for the mix up, the first patch is no good.

comment:2 Changed 9 years ago by Baptiste <baptiste.goupil@…>

I like it !

comment:3 Changed 9 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

Does look interesting

Changed 9 years ago by philippe.raoult@…

comment:5 Changed 9 years ago by philippe.raoult@…

The patch now supports all date/time fields and ForeignKey. I still need to work on ManyToMany fields.

comment:6 Changed 9 years ago by mtredinnick

  • Patch needs improvement set
  • Triage Stage changed from Design decision needed to Accepted

Fields that are not editable are the readonly fields. We don't need an extra attribute for that.

Also, both empty and NULL fields should just display as the empty string (i.e. not at all). The difference isn't important for a read only field -- it's not like you can change it.

This looks like a reasonable idea, but it's over-engineered at the moment. It should also be usable for all forms, not just the admin interface.

comment:7 Changed 9 years ago by adrian

  • Version changed from new-admin to SVN

comment:8 Changed 8 years ago by C. Scott Ananian <cscott@…>

  • Cc cscott@… added

An attribute of this sort is badly needed: I have a few foreign key objects which reference tens of thousands of possible objects. Generating a select widget for this field kills the server and browser, but I'd still like to see its current value when I'm browsing data.

I disagree with mtredinnick that 'editable' should be overloaded for 'readonly'. There are two separate concepts here, although calling them 'hidden' and 'editable' would be a reasonable solution.

comment:9 Changed 8 years ago by brosner

  • Keywords nfa-someday added

The behavior of editable is not going to change. This can be accomplish by writing your own widget that makes a text input box readonly or just display the value and have a hidden input box. newforms-admin gives you the ability to override any database field's formfield and/or widget. In all honesty this should be closed once newforms-admin is merged, but it might be a better case to write a patch that includes this widget in newforms-admin.

comment:10 Changed 8 years ago by Thomas Steinacher <tom@…>

  • Cc tom@… added

+1 for read-only admin fields

comment:11 Changed 7 years ago by mrts

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #342 , albeit a little different.

Note: See TracTickets for help on using tickets.
Back to Top