'选项'对象没有属性' get_all_field_names'

时间:2016-10-24 07:35:27

标签: python django python-2.7 django-admin

在我的django-admin中,我试图让model不可编辑。

所以,我overridingget_readonly_fields 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()

'Options' object has no attribute 'get_all_field_names'

但我面临一个错误。我在这里粘贴错误。

bindVar

任何想法为什么?

1 个答案:

答案 0 :(得分:16)

可能是因为你正在使用django 1.10。此版本中get_all_field_names was deleted。使用get_fields

def get_readonly_fields(self, request, obj=None):
    return [f.name for f in self.model._meta.get_fields()]

或完全兼容的版本

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)
    )))