Reputation: 9392
In my django-admin
, I am trying to make a model
non-editable.
So, I am overriding
a method get_readonly_fields
of admin.ModelAdmin
.
Here is my Code
@admin.register(SMSTemplate)
class SMSTemplateAdmin(admin.ModelAdmin):
list_display=['title', 'json', 'note']
formfield_overrides = {
JSONField: {'widget': PrettyJSONWidget }
}
def has_delete_permission(self, request, obj=None):
return False
def get_readonly_fields(self, request, obj=None):
return self.model._meta.get_all_field_names()
But I am facing an error. I am pasting the error here.
'Options' object has no attribute 'get_all_field_names'
Any Idea why ?
Upvotes: 10
Views: 11329
Reputation: 15400
It is probably because you are using django 1.10. get_all_field_names
was deleted in this version. Use get_fields
def get_readonly_fields(self, request, obj=None):
return [f.name for f in self.model._meta.get_fields()]
Or for full compatible version
from itertools import chain
def get_readonly_fields(self, request, obj=None):
return list(set(chain.from_iterable(
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
for field in self.model._meta.get_fields()
# For complete backwards compatibility, you may want to exclude
# GenericForeignKey from the results.
if not (field.many_to_one and field.related_model is None)
)))
Upvotes: 25