2 ms·
Once you figure out how to do it the Django admin actually has really neat support for read-only views of data. The trick is to implement these methods on you
by simonw 4y ago
Once you figure out how to do it the Django admin actually has really neat support for read-only views of data.
The trick is to implement these methods on your ModelAdmin subclass: https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_add_permission https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#dja...
You can make has_add_permission(), has_change_permission() and has_delete_permission() all return False, while has_view_permission() returns True.
The readonly_fields and related get_readonly_fields() method are very useful too.
I wrote up another related trick in this TIL: https://til.simonwillison.net/django/extra-read-only-admin-information https://til.simonwillison.net/django/extra-read-only-admin-i...
- irjustin 4y agoThanks, but the goal isn't to prevent editing. It's to prevent accidental editing. Staff need to be able to change things but 95% of the time they're simply viewing data to help answer questions, etc.
- simonw 4y agoYou can do that too with some more tricks. It's possible to register more than one ModelAdmin for a given model - so you could have a read only one which staff spend most of their time in, but that then links to the writable one when they need to make changes. You could even register multiple admin sites: /admin/ showing read-only data and /admin-write/ that allows it to be edited.