I have a tabular inline model in the Django admin. I need 1 of the fields to not be changeable after it has been created, but setting it as readonly (via readonly_fields) wh
This code work perfectly according to your requirements.
Actually i got this answer from my own question but specific to my problem and i removed some lines related to my problem. And credit goes to @YellowShark. Check here my question.
Once you created new inline then you will be not able to edit existing inline.
class XYZ_Inline(admin.TabularInline):
model = YourModel
class RequestAdmin(admin.ModelAdmin):
inlines = [XYZ_Inline, ]
# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets = []
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)
if isinstance(inline, XYZ_Inline):
for form in inline_admin_formset.forms:
#Here we change the fields read only.
form.fields['some_fields'].widget.attrs['readonly'] = True
inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets
You can add only new inline and read only all existing inline.