shevdenvla
shevdenvla

Reputation: 55

How allow to fit read_only field for new record in TabularInline class in Django

I want to have a field readonly when record already saved but if admin adds a new record field must to be editable.

How I can achieve it?

Upvotes: 0

Views: 50

Answers (1)

You can use ModelAdmin's get_readonly_fields method for this purpose. When the object is created, the obj is set to None. By overriding the method, you can change the read_only fields if obj is present or not.

def get_readonly_fields(self, obj):
    if obj:
       return ['field_1', 'field_2']
    else:  # When object is created
       return [] # no editable field

References:

Upvotes: 1

Related Questions