Set readonly fields only for view details on Django Admin


In some condition we want to make fields became read-only on “view details” in Django Admin. That’s mean, for adding new records, this fields must be not in read-only mode. To solve this problems, we can use get_readonly_fields().

Here is an example:

1
2
3
4
5
6
7
class InsuranceAdmin(admin.ModelAdmin):
    list_display = (‘id’, ’email’, ‘brand’, ‘website’)

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return (’email’)
        return ()

This code will give email fields editable on “Add” pages but become read-only on “View” pages.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.