Read-only attributes for fields/forms/whatever

150 views
Skip to first unread message

David Cramer

unread,
Jul 27, 2008, 8:21:25 PM7/27/08
to Django developers
Lately I've had the need to use editable=False for certain things,
beyond my usual auto now dates and similar things. Now while I don't
want these edited via the admin, it's sometimes useful to be able to
see the value (e.g. an IP Address).

My proposal, is that there be some sort of read-only flag, whether
it's via newforms + newforms admin, or on the model itself. It makes
sense in regards to the administration as administration isn't just
for editing data, its also used for viewing data a lot of the time. I
haven't thought on an implementation much (beyond newforms specific
usage), but I thought I'd throw the idea around.

Julien Phalip

unread,
Jul 27, 2008, 8:31:33 PM7/27/08
to Django developers
Hi David,

That's quite funny, I was thinking about that too lately.

How about something like:

class MyModelAdmin (admin.ModelAdmin)
fields = ['name', 'bio', 'url', 'date_signup']
readonly = ['date_signup']

Concerning implementation, the form is built with modelform_factory or
inlineformset_factory in the admin.options.py
So far, I see two possibilities:
- programmatically subtract readonly fields from the fields/fieldsets
options just before calling the factory.
- only ignore the input widgets in the template and display the value
instead.

Julien

David Cramer

unread,
Jul 27, 2008, 8:33:37 PM7/27/08
to django-d...@googlegroups.com
I think that could work. But what would work better is if you simply used read-only widgets. So everything stays as it would be normally in fieldsets (ordering and what not) but it tries to find a read-only widget.

e.g.

ReadOnlyChoiceField, ReadOnlyTextInput, they'd simply output a similar display to the default, but without edit capabilities, and then when doing save or something they could be ignored in the list so that it isn't possible to save them.
--
David Cramer
Director of Technology
iBegin
http://www.ibegin.com/

Malcolm Tredinnick

unread,
Jul 27, 2008, 8:38:30 PM7/27/08
to django-d...@googlegroups.com

On Sun, 2008-07-27 at 17:21 -0700, David Cramer wrote:
> Lately I've had the need to use editable=False for certain things,
> beyond my usual auto now dates and similar things. Now while I don't
> want these edited via the admin, it's sometimes useful to be able to
> see the value (e.g. an IP Address).

We've discussed this in the past (Adrian, Jacob and I first discussed it
over dinner back in 2006) and implementation was pushed off pending
newforms-admin being completed. The natural way to do it is that
editable=False fields can be included in the "fields" list for an admin.
They won't display as a form field, just as plain text. Implementation
and final design decisions will have to wait until after 1.0 now, but
it's certainly something that has its uses. Doesn't need any extra
flags, since we already have "editable". Just needs a way to say
"include this in the display", which is the "fields" list.

Regards,
Malcolm


Julien Phalip

unread,
Jul 27, 2008, 8:48:44 PM7/27/08
to Django developers
I can see 2 cases here:

- fields that have editable=False and that are present in the fields/
fieldsets admin options. Those would automatically rendered read-only.
- fields that are intrinsically editable, but that the developer, for
some, doesn't want to be edited by some administrators.

NFA lets you create several admin configurations for the same site.
You may want to have some fields edited by some administrators but not
by other administrators.

Having a 'readonly'-like option in ModelAdmin would allow that fine-
grained customization.

On Jul 28, 10:38 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

David Cramer

unread,
Jul 27, 2008, 9:07:05 PM7/27/08
to django-d...@googlegroups.com
I agree that since the approach of NFA is to make the admin more modular, this makes sense as a global level (editable=False) and a NFA instance level (read-only/editable fields markers).

Don Spaulding

unread,
Jul 28, 2008, 4:20:53 PM7/28/08
to django-d...@googlegroups.com

On Sun, 2008-07-27 at 17:21 -0700, David Cramer wrote:
Lately I've had the need to use editable=False for certain things,
beyond my usual auto now dates and similar things. Now while I don't
want these edited via the admin, it's sometimes useful to be able to
see the value (e.g. an IP Address).

We have a similar use case at work where we want to see field values from related objects that are raw_id_fields in the admin.  It would be spectacular if we could do something like...

BookAdmin(admin.ModelAdmin):
    readonly= ['isbn', 'author__name']

... to include those values that span relationships.  I've put no thought into how this would look in code, it just struck me as being related to the readonly feature you've proposed.

On Sun, Jul 27, 2008 at 7:38 PM, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:

The natural way to do it is that
editable=False fields can be included in the "fields" list for an admin.
They won't display as a form field, just as plain text.

 
This seems like a simple enough solution, but as Julien pointed out, it doesn't appear that this method will solve the case of "read-only in one admin, editable in another"


--
Don
Reply all
Reply to author
Forward
0 new messages