'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?



Solution 1:[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:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syed Muhammad Dawoud Sheraz