Dynamically setting readonly_fields in django admin
django, django-admin, python, python-2.7
Solution
You can inherit the function get_readonly_fields() in your admin and set readonly fields according your model's certain field value
class TranslationAdmin(admin.ModelAdmin):
...
def get_readonly_fields(self, request, obj=None):
if obj.certainfield == something:
return ('field1', 'field2')
else:
return super(TranslationAdmin, self).get_readonly_fields(request, obj)
I hope it will help you.
Problem
Can I change `readonly_fields` in my `TranslationAdmin` class dependent on the value of a certain field in the `Translation` being viewed? If so, how would I do that? The only thing I've come up with is to make a widget that looks at the `Translation` and decides whether to be a readonly widget or not, but that seems like overkill for this.