我有一个条件,我希望阻止在我的应用程序中编辑某些django模型中的任何输入记录。同时我希望允许相同的添加权限,并允许查看模型的现有记录的权限。我知道ModelAdmin中的readonly_fields选项。但我正在寻找一个通用选项,以便可以为某些模型继承它。
class ReadOnlyAdmin(admin.ModelAdmin):
readonly_fields = []
def get_readonly_fields(self, request, obj=None):
return list(self.readonly_fields) + \
[field.name for field in obj._meta.fields]
def has_add_permission(self, request, obj=None):
return True #False originally
def has_delete_permission(self, request, obj=None):
return False
class CompanyAdmin(ReadOnlyAdmin):
list_display = ('name','website')
如果has_add_permission返回True,则返回错误:
AttributeError at /admin/tracker/company/add/ 'NoneType' object has no attribute '_meta' Request Method: GET Request URL: http://myhost:8000/admin/tracker/company/add/ Django Version: 1.6.5 Exception Type: AttributeError Exception Value: 'NoneType' object has no attribute '_meta'
答案 0 :(得分:0)
我认为覆盖只读方法是显而易见的路径:
class ReadOnlyAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# your logic
class YourAdmin(ReadOnlyAdmin):
pass