Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Options' object has no attribute 'get_all_field_names'

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 ?

like image 604
Adil Malik Avatar asked Oct 24 '16 07:10

Adil Malik


1 Answers

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)
    )))
like image 83
Sardorbek Imomaliev Avatar answered Nov 13 '22 18:11

Sardorbek Imomaliev