Non-editable (but visible) fields in admin?

1,301 views
Skip to first unread message

Rubens Altimari

unread,
Jul 16, 2009, 11:11:20 PM7/16/09
to django...@googlegroups.com
Hello,

Is there a way to show model fields in admin, but prevent them from
being edited? Using 'editable=False' in the model won't do, since then
the field simply is not displayed.

Also, on a related topic: is there a simple way to allow the user to
set a certain field when *creating* a new record, but then "lock" the
field from further changes? Similar to the previous question, only I
wanted to allow the field to be set when creating the record.

Thanks in advance,
Rubens

Shawn Milochik

unread,
Jul 17, 2009, 11:13:18 PM7/17/09
to django...@googlegroups.com

The short answer to part one is "yes," because you can customize
everything in Django. However, I don't know if it's trivial to modify
the admin in that way, and that's really not the kind of thing the
admin was intended for. You should probably just make a form for this
functionality. It would take about a minute, and the view wouldn't
take much longer.

For the second part, certainly. Just override the save function of the
model. If the object already has a primary key, then don't save those
fields. There may be a more elegant way to check whether it's already
been saved than checking the instance.id, so maybe someone else will
chime in.

Shawn


dalembertian

unread,
Jul 19, 2009, 12:49:55 PM7/19/09
to Django users
Thanks for the reply, Shawn.

I did find a solution to my first topic at http://www.djangosnippets.org/snippets/937/
which attend most of my needs. I think that having a 'readonly'
attribute for fields would be a nice feature for the admin - providing
a whole new form just because of that sounds like a waiste, to me...

As to the second part, yes, I can easily manipulate the save() method,
but I was wanting something that didn't require an actual post. I'm
now using the snippet above to get what I need!

Regards,
Rubens

Jesus Peña Siguas

unread,
Dec 8, 2014, 12:37:20 AM12/8/14
to django...@googlegroups.com, rub...@altimari.com.br
readonly_fields=('campo',)
Reply all
Reply to author
Forward
0 new messages