Is there a way to make a model read-only in the django admin? but I mean the whole model. So, no adding, no deleting, no changing, just see the objects and the fields, everything as read-only?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- TypeError: 'BaseQuery' object is not calla
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
ModelAdmin provides the hook get_readonly_fields() - the following is untested, my idea being to determine all fields the way ModelAdmin does it, without running into a recursion with the readonly fields themselves:
then subclass/mixin this admin whereever it should be a read-only admin.
For add/delete, and to make their buttons disappear, you'll probably also want to add
P.S.: In ModelAdmin, if has_change_permission (lookup or your override) returns False, you don't get to the change view of an object - and the link to it won't even be shown. It would actually be cool if it did, and the default get_readonly_fields() checked the change permission and set all fields to readonly in that case, like above. That way non-changers could at least browse the data... given that the current admin structure assumes view=edit, as jathanism points out, this would probably require the introduction of a "view" permission on top of add/change/delete...
EDIT: regarding setting all fields readonly, also untested but looking promising:
EDIT: Here's another one
I had a similar scenario where:
1. Overriding the Change View
Because it's possible to override the
change_view()
in a ModelAdmin, we can exploit that to prevent the editing of model instances once they have been created. Here's an example I've used:2. Conditionally Change Edit Permissions
I also realized that the docs interpret the result of
ModelAdmin.has_change_permission()
in different ways:Meaning I could check whether
obj
isNone
, in which case I returnTrue
, otherwise I returnFalse
, and this in effect allows users to view the change-list, but not be able to edit nor view the change_form after the model instance is saved.Though am thinking this might also override any
MODEL_can_change
permissions allowing unwanted eyes from viewing the change-list?The selected answer doesn't work for Django 1.11, and I've found a much simpler way to do it so I thought I'd share:
You may customize your
ModelAdmin
classes with thereadonly_fields
attribute. See this answer for more.According to my test on Django 1.8 we can not use following as noted on answer #3 but it works on Django 1.4:
Can be something like:
As "view permissions" will not make it into Django 1.11, unfortunately, here's a solution that makes your ModelAdmin read-only by making both saving model changes and adding model history log entries a no-op.
(NOTE: Don't mistake the
false
no-op helper with theFalse
builtin. If you don't sympathize with the helper function outside the class move it into the class, call itno_op
or something else, or override the affected attributes by usualdef
s. Less DRY, but if you don't care...)This will:
It will not:
Note that
get_all_field_names
(as mentioned in the accepted answer) was removed in Django 1.10. Tested with Django 1.10.5.