Opened 18 years ago

Closed 16 years ago

#3990 closed (duplicate)

readonly fields for admin

Reported by: philippe.raoult@… Owned by: nobody
Component: contrib.admin Version: dev
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: no UI/UX: no

Description

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 18 years ago.
admin_readonly_fields_v1.diff (4.3 KB ) - added by philippe.raoult@… 18 years ago.
admin_readonly_fields_v1.1.diff (7.5 KB ) - added by philippe.raoult@… 18 years ago.

Download all attachments as: .zip

Change History (14)

by philippe.raoult, 18 years ago

by philippe.raoult@…, 18 years ago

comment:1 by philippe.raoult@…, 18 years ago

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

comment:2 by Baptiste <baptiste.goupil@…>, 18 years ago

I like it !

comment:3 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedDesign decision needed

Does look interesting

by philippe.raoult@…, 18 years ago

comment:5 by philippe.raoult@…, 18 years ago

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

comment:6 by Malcolm Tredinnick, 18 years ago

Patch needs improvement: set
Triage Stage: Design decision neededAccepted

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 by Adrian Holovaty, 17 years ago

Version: new-adminSVN

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

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 by Brian Rosner, 17 years ago

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 by Thomas Steinacher <tom@…>, 16 years ago

Cc: tom@… added

+1 for read-only admin fields

comment:11 by mrts, 16 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #342 , albeit a little different.

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