Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_readonly_fields in a TabularInline class in Django?

I'm trying to use get_readonly_fields in a TabularInline class in Django:

class ItemInline(admin.TabularInline):
    model = Item
    extra = 5

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['name']

        return self.readonly_fields

This code was taken from another StackOverflow question: Django admin site: prevent fields from being edited?

However, when it's put in a TabularInline class, the new object forms don't render properly. The goal is to make certain fields read only while still allowing data to be entered in new objects. Any ideas for a workaround or different strategy?

like image 508
mpso Avatar asked Jul 17 '11 23:07

mpso


1 Answers

Careful - "obj" is not the inline object, it's the parent. That's arguably a bug - see for example this Django ticket

like image 78
Danny W. Adair Avatar answered Sep 28 '22 03:09

Danny W. Adair