[Fixed]-Using get_readonly_fields in the admin for a model with a @property

1👍

It seems the only way to get this to work is to explicitly add xyz to the list of read-only fields.

class MyModelAdmin(admin.ModelAdmin):
    readonly_fields = ("xyz", ...)
    ...
    def get_readonly_fields(self, request, obj=None):
        if <condition>:
            ro = [f.name for f in self.model._meta.fields]
            ro.append('xyz') # <=============
            return ro
        return self.readonly_fields
👤TAH

0👍

Properties are different from fields. You’ve defined xyz as a property, which is not supported as editable by the Django Admin. Only fields on the model can be members of the readonly_fields property of your admin. You should find that xyz is not a member of self.model._meta.fields and thus would never be returned by get_readonly_fields.

You should also include either readonly_fields or get_readonly_fields.

Leave a comment