Readonly for existing items only in Django admin inline

前端 未结 7 1270
长发绾君心
长发绾君心 2020-12-13 14:27

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

7条回答
  •  囚心锁ツ
    2020-12-13 15:03

    Here's a better read-only widget that I've used before:
    https://bitbucket.org/stephrdev/django-readonlywidget/

    from django_readonlywidget.widgets import ReadOnlyWidget
    
    class TestAdmin(admin.ModelAdmin):
        def formfield_for_dbfield(self, db_field, **kwargs):
            field = super(TestAdmin, self).formfield_for_dbfield(db_field, **kwargs)
            if field:
                field.widget = ReadOnlyWidget(db_field=db_field)
            return field
    

提交回复
热议问题