Django

Code

Ticket #342 (closed: fixed)

Opened 5 years ago

Last modified 2 weeks ago

Add a way for fields to be displayed in the admin without being editable

Reported by: Bless Assigned to: brosner
Milestone: 1.2 Component: django.contrib.admin
Version: newforms-admin Keywords: djangocon
Cc: josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com, reinis.veips@wot.lv, francois@hop2it.be Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

Description

When editable=False option is used then that field isn't showed in the form.

But it should be an option for show a field without can to editing it.

Attachments

readonly-admin.diff (14.6 kB) - added by Alex on 10/24/09 13:11:26.
Initial patch.
readonly-admin.2.diff (16.2 kB) - added by Alex on 10/24/09 14:26:46.
Added documentation.
readonly-admin.3.diff (18.5 kB) - added by Alex on 10/24/09 16:51:33.
Added validation (and tests for the validation), only needs tests now.
readonly-admin.4.diff (21.9 kB) - added by Alex on 10/26/09 18:59:14.
Added tests.
readonly-admin.5.diff (21.4 kB) - added by fest on 11/09/09 10:33:21.
Fixed label tag generation to use pretty_name instead of capfirst, which now produces pretty field names when underscores are used.
readonly-admin.6.diff (22.1 kB) - added by Alex on 12/11/09 20:44:43.
readonly-admin.7.diff (24.9 kB) - added by Alex on 12/11/09 21:58:19.
Pretty printing
catch_value_error.diff (0.6 kB) - added by hejsan on 03/01/10 05:40:47.
An extra patch to catch ValueError? in case of ManyToMany? field

Change History

08/19/05 15:52:39 changed by adrian

  • summary changed from editable option - suggestion to Add a way for fields to be displayed in the admin without being editable.

01/17/07 22:14:32 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

Personally, I think it's a good idea too (something like readonly=True).

What's the word from the men upstairs?

03/09/07 06:00:28 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

Yes, this is useful. However, they should just be displayed inline (perhaps surrounded with some styling), without using an input widget with readonly="true" or anything like that. These are truly non-editable fields, not just temporarily disabled. So insert them into the form as text.

04/08/07 10:23:02 changed by philippe.raoult@gmail.com

09/16/07 06:26:37 changed by ubernostrum

  • status changed from new to closed.
  • resolution set to wontfix.

newforms-admin will obsolete this; you will, in theory, be able to write your own customized interface to do whatever you'd like, and in the current admin this goes against the explicitly-stated philosophy of the admin interface.

05/11/08 13:33:20 changed by anonymous

  • status changed from closed to reopened.
  • version changed from 1.0 to newforms-admin.
  • resolution deleted.

newforms-admin will not obsolete this. To have non editable fields in admin, even with newforms-admin, you have to inherit from the template and have some "if field in readonly_fields: skip" logic, thus hardcoding the list of readonly fields in a non-standard place, which is sub-optimal and doesn't scale. Having an option in ModelAdmin? like readonly_fields=... is the proper way to handle this.

05/11/08 23:07:52 changed by ubernostrum

  • status changed from reopened to closed.
  • resolution set to wontfix.

No, really, you'll be able to write this in pure Python by using widget attributes when you set up the form. Plus, once again, it goes against the stated philosophy of the admin interface, which is that it's for actually administering data, not merely viewing it.

09/08/08 10:56:54 changed by simon

  • keywords set to djangocon.
  • status changed from closed to reopened.
  • resolution deleted.

I emphatically disagree - in practical terms this comes up all the time. A bunch of examples:

* You set the "date added" field automatically when something is created but do not want editors to be able to change it because that would break your date-based URLs - but they still need to be able to see when it was created. * You're implementing finely grained permissions where only certain users can put something live on the site (by changing the published flag for example) - a very basic workflow mechanism. Lower level users still need to be able to see if something is published or not. * You don't want people to be able to edit the slug once something has been created, again to prevent URLs from breaking. * Items in your database were created by importing data from a third party (e.g. a sports feed, or pulling in photos from a Flickr photostream). You want people to be able to edit certain fields that are specific to your site, but not edit the fields that are determined by that external data source. * You want to automatically save "who created this", and allow that to be viewed but not edited later. * You're implementing a comment moderation interface. Users of that interface should not be able to edit the IP address for a comment, but should still be able to view it.

I've needed to do all of the above in the past. They all count as "administering" data, but all require certain fields to be visible but not editable.

(Plus it got a cheer at DjangoCon, so it should definitely be discussed further as a potential feature for a future release).

09/08/08 10:57:42 changed by simon

Here's that list properly formatted:

  • You set the "date added" field automatically when something is created but do not want editors to be able to change it because that would break your date-based URLs - but they still need to be able to see when it was created.
  • You're implementing finely grained permissions where only certain users can put something live on the site (by changing the published flag for example) - a very basic workflow mechanism. Lower level users still need to be able to see if something is published or not.
  • You don't want people to be able to edit the slug once something has been created, again to prevent URLs from breaking.
  • Items in your database were created by importing data from a third party (e.g. a sports feed, or pulling in photos from a Flickr photostream). You want people to be able to edit certain fields that are specific to your site, but not edit the fields that are determined by that external data source.
  • You want to automatically save "who created this", and allow that to be viewed but not edited later.
  • You're implementing a comment moderation interface. Users of that interface should not be able to edit the IP address for a comment, but should still be able to view it.

09/08/08 11:19:58 changed by simon

Intercepting the form, finding the field that you want to make read-only and then modifying its widget is a pretty nasty solution for something that's actually pretty common. It also seems a bit dirty to have a form field for something with a widget that doesn't actually let you edit it - if it's a form field, it should be editable. Widgets that display rather than edit seem pretty hacky to me.

09/08/08 21:21:14 changed by jcroft

+1 for "readonly_fields" in the admin options (or another name, if someone's got a better one). I need this ALL the time.

09/09/08 11:22:28 changed by bkroeze

+1 here, Satchmo really needs this functionality for some of the incredible stuff we've got brewing in the store admin interface. Also, it really did get a large majority cheer at DjangoCon when I proposed it during the "give us your pony request" section.

09/10/08 10:26:34 changed by danfairs

  • cc set to dan.fairs@gmail.com.

09/15/08 14:02:49 changed by nagyv

I have the feeling that simon's list in large part contains data that shouldn't be edited once the item is created. If this is the case then I would rather thing about a readonly=True model option. Something similar to the DateField?'s auto_now_add argument. Basically, you never want to edit a DateField? with auto_now_add=True. That is there is rationale to have it specified in the model instead of admin option.

Moreover, even if it might be easier to define a readonly_fields in your admin class, logically this is about presenting your data, so it should be in you form. This way you can easily order the fields as well.

So my idea would be to implement it as a widget + an option in model definition.

10/08/08 12:42:08 changed by cainmatt@gmail.com

I have a small patch in my personal version of django which adds editable=forms.VIEW_ONLY as a model field option.

This seems along the lines discussed above.

My patch still has these issues however: * the ViewOnlyWidget? currently just calls force_unicode(value) which is not appropriate for all field types (eg. ForeignKeys? are rendered as the id only) * the admin add form will only show the default value and not an input box. To support setting a value on create would require an additional option such as editable=forms.IMMUTABLE since some of the use cases above should allow setting an initial value and some should not

Send me an email if you would like the patch. It is just to django/db/models/fields/init.py, django/forms/forms.py and django/forms/widgets.py

03/22/09 04:01:46 changed by legutierr

If no one is working on this at the moment, I would like to give it a shot. My understanding is that this enhancement has made it into the 1.1 roadmap as a "maybe" feature.

* further discussion of this bug at #820, #7150, #8947

03/31/09 06:26:45 changed by mrts

  • milestone set to 1.2.

Re-targeting for 1.2.

04/18/09 06:08:35 changed by mrts

See also #3990 (closed as duplicate of this) that has a somewhat different approach -- fields can be added but not changed later.

04/20/09 15:33:00 changed by legutierr

I jotted down some ideas about how this feature can be tackled; check it out at ReadOnlyAdmin. I will extend that wiki page with some implementation specs once I finish the project that I am working on now.

It is my intent to do any implementation work required, once I have to opportunity.

05/07/09 05:03:37 changed by apollo13

  • cc changed from dan.fairs@gmail.com to dan.fairs@gmail.com, apollo13.

07/29/09 01:40:27 changed by anonymous

  • cc changed from dan.fairs@gmail.com, apollo13 to dan.fairs@gmail.com, apollo13, taavi@taijala.com.

07/29/09 10:07:50 changed by chromano

  • cc changed from dan.fairs@gmail.com, apollo13, taavi@taijala.com to dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com.

08/04/09 12:02:25 changed by veena

I think it should be easy to understand the behaviour from name of the attributes/list.

I'm not native english speaker but readonly doesn't give me clear view if I can fill field in create form and it's only disable for editing or also creating is disabled. And then I miss the feature of hidden field.

Proposals in ReadOnlyAdmin seems to me too complex and combination with permissions makes it even more complex. I think first behaviour should be straightforward. Then if we try it in admin and in future there will be some row level persmissions we could combine it.

I propose these two options in ModelAdmin? class:

  • list_disable_edit = [] or disable_edit_fields = []
  • list_disable_create = [] or disable_create_fields = []

list_disable_edit for fields I don't want to make them editable but I should fill it once in create form and then see only value in edit form.

list_disable_create for eg. automatically filled fields by imports scripts or own save() methods. In create form you could see some text from help_text like: "This is automatically filled field." or so on. If you don't also set that field in disable_edit_fields you are able to edit it further if you want.

It's for another discussion how it would be implemented on the widget side eg.: disable="disable", readonly="readonly" or only value as a text.

Maybe I'm missing something than apologize me, I'm not contributor to django but I'm using it heavy.

08/13/09 07:24:16 changed by guettli

  • cc changed from dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com to dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de.

08/15/09 08:56:58 changed by anonymous

  • cc changed from dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de to cg@webshox.org.

08/15/09 08:57:16 changed by anonymous

  • cc changed from cg@webshox.org to dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org.

08/18/09 02:39:53 changed by anonymous

  • cc changed from dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org to dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com.

08/22/09 23:04:27 changed by joshuajonah

  • cc changed from dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com to josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com.

08/27/09 11:06:24 changed by nix

  • cc changed from josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com to josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net.

08/27/09 11:33:30 changed by anonymous

  • cc changed from josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net to josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com.

09/20/09 14:34:52 changed by Rupe

Something to keep in mind are fields like slug and primary key fields that may be editable only for new records. This is a little different from just saying that a field is solidly read only. These fields would be read only but only for the change view.

I'm not quite sure how best to handle the difference though. Maybe display_only=True/False and display_only_change=True/False. I personally like the term 'display only' over 'read only'.

10/21/09 09:58:24 changed by jezdez

For the record, there is a pretty nice implementation here: http://bitbucket.org/stephrdev/django-readonlywidget/src/

10/24/09 13:11:26 changed by Alex

  • attachment readonly-admin.diff added.

Initial patch.

10/24/09 13:11:41 changed by Alex

  • owner changed from nobody to Alex.
  • needs_docs set to 1.
  • has_patch set to 1.
  • status changed from reopened to new.
  • needs_tests set to 1.

10/24/09 14:26:46 changed by Alex

  • attachment readonly-admin.2.diff added.

Added documentation.

10/24/09 14:26:54 changed by Alex

  • needs_docs deleted.

10/24/09 16:51:33 changed by Alex

  • attachment readonly-admin.3.diff added.

Added validation (and tests for the validation), only needs tests now.

10/26/09 18:59:14 changed by Alex

  • attachment readonly-admin.4.diff added.

Added tests.

11/09/09 10:33:21 changed by fest

  • attachment readonly-admin.5.diff added.

Fixed label tag generation to use pretty_name instead of capfirst, which now produces pretty field names when underscores are used.

11/09/09 10:35:10 changed by fest

  • cc changed from josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com to josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com, reinis.veips@wot.lv.

11/11/09 07:06:38 changed by frans

  • cc changed from josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com, reinis.veips@wot.lv to josh@joshuajonah.com, dan.fairs@gmail.com, apollo13, taavi@taijala.com, chromano@gmail.com, hv@tbz-pariv.de, cg@webshox.org, rlaager@wiktel.com, listuser@peternixon.net, russryba@gmail.com, reinis.veips@wot.lv, francois@hop2it.be.

11/15/09 14:29:06 changed by yed@seznam.cz

  • needs_better_patch set to 1.

This patch doesn't work with fix for this bug: http://code.djangoproject.com/ticket/8164 - readonly_fields are moved to end of form, if fields are not defined in ModelAdmin? but in ModelForm?'s Meta class.

12/11/09 20:44:43 changed by Alex

  • attachment readonly-admin.6.diff added.

12/11/09 21:58:19 changed by Alex

  • attachment readonly-admin.7.diff added.

Pretty printing

12/12/09 13:04:17 changed by brosner

  • owner changed from Alex to brosner.
  • status changed from new to assigned.

12/22/09 12:29:01 changed by brosner

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [11965]) Fixed #342 -- added readonly_fields to ModelAdmin?. Thanks Alex Gaynor for bootstrapping the patch.

ModelAdmin? has been given a readonly_fields that allow field and calculated values to be displayed alongside editable fields. This works on model add/change pages and inlines.

03/01/10 05:39:51 changed by hejsan

  • status changed from closed to reopened.
  • resolution deleted.

This breaks if a ManyToMany? field is specified as readonly.

This is because the contents() function that is called in the fieldset.html template tries to display the value of the field but gets a ValueError? if it is a ManyToManyField? because they can't be used: "[Model name] instance needs to have a primary key value before a many-to-many relationship can be used"

Just need to add ValueError? to the catch as seen in the patch I attached, called catch_value_error.diff.

03/01/10 05:40:47 changed by hejsan

  • attachment catch_value_error.diff added.

An extra patch to catch ValueError? in case of ManyToMany? field

03/01/10 06:44:48 changed by kmtracey

  • status changed from reopened to closed.
  • resolution set to fixed.

Please open new tickets for follow-on problems rather than re-opening a fixed ticket.


Add/Change #342 (Add a way for fields to be displayed in the admin without being editable)




Change Properties
Action