How to make some Django Inlines Read Only

I'm using django admin and adding a TabularInline to my ModelAdmin class. Think Author and the inline is Books. How do I make some of those inlines appear read only and some appear editable based on certain characteristics of the model instance for each specific inline? Say I wanted all the books written before 2001 to be read only. And all the books written after that to be editable.

I've tried overriding has_change_permission() and using the obj param, but it is receiving the parent and not the instance of this specific inline.

Have you tried overriding the get_readonly_fields methods on your tabularinline?

Example:

class AuthorBookInline(admin.TabularInline):
    model = Book
    extra = 0

    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super().get_readonly_fields(request, obj)
        if obj:
            if obj.created_ts.year <= 2001:
                readonly_fields.extend(self.fields)
        return readonly_fields
Back to Top