我正在尝试将所有字段设为只读而不明确列出它们。
类似的东西:
class CustomAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
return self.readonly_fields
return self.fields
问题是此时未设置CustomAdmin.fields
。
有什么想法吗?
答案 0 :(得分:34)
小心,self.model._meta.fields不一定是CustomAdmin所拥有的字段!
“管理员的所有字段”看起来更像是这样:
from django.contrib import admin
from django.contrib.admin.utils import flatten_fieldsets
class CustomAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
return self.readonly_fields
if self.declared_fieldsets:
return flatten_fieldsets(self.declared_fieldsets)
else:
return list(set(
[field.name for field in self.opts.local_fields] +
[field.name for field in self.opts.local_many_to_many]
))
答案 1 :(得分:18)
好的,现在有了这个:
class CustomAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# ...
return [f.name for f in self.model._meta.fields]
仍在寻找一种不那么丑陋的方式。
答案 2 :(得分:10)
您可以遍历模型元字段:
def get_readonly_fields(self, request, obj=None):
if obj:
self.readonly_fields = [field.name for field in obj.__class__._meta.fields]
return self.readonly_fields
答案 3 :(得分:5)
对于内联(制表符或堆栈)
def get_readonly_fields(self, request, obj=None):
fields = []
for field in self.model._meta.get_all_field_names():
if field != 'id':
fields.append(field)
return fields
def has_add_permission(self, request):
return False
答案 4 :(得分:3)
这对我来说对Django 1.10起作用
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
return self.readonly_fields
return list(set(
[field.name for field in self.opts.local_fields] +
[field.name for field in self.opts.local_many_to_many]
))
答案 5 :(得分:3)
您可以这样做:
{ "occupants": [{"ref_id":"abc123", "ref_type": "Dog"}, {"ref_id":"gdr736", "ref_type":"Cat"}] }
答案 6 :(得分:1)
如果有人还在寻找更好的方法,你可以这样使用:
@admin.register(ClassName)
class ClassNameAdmin(admin.ModelAdmin):
readonly_fields = [field.name for field in ClassName._meta.fields]
ClassName 是您的模型类。
答案 7 :(得分:0)
我的要求与此类似。我需要只有一个字段才能显示为只读。这很好用:
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 1
fields = ['choice_text', 'votes']
readonly_fields = ['votes']
class QuestionAdmin(admin.ModelAdmin):
#fields = ['pub_date', 'question_text']
fieldsets = [
(None, {'fields': ['question_text']}),
('Date Information', {'fields': ['pub_date']}),
]
search_fields = ['question_text']
inlines = [ChoiceInline]
参考:C:\ Python27 \ Lib \ site-packages \ django \ contrib \ admin \ options.py
答案 8 :(得分:0)
@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
...
readonly_fields = ["father", "mother", "spouse"]
参考: https://books.agiliq.com/projects/django-admin-cookbook/en/latest/changeview_readonly.html
答案 9 :(得分:0)
假设您已将 user_mode 定义为;
Admin, Customers and Staff
如果您想拒绝员工(当然还有客户)删除客户、产品、订单等的权限...
您的代码运行;
def get_readonly_fields(self, request: HttpRequest, obj=None):
if request.user.user_mode != "Admin":
return self.readonly_fields + ['user_mode']
return super().get_readonly_fields(request, obj)
其中 user_mode = 保存用户类型的模型字段。
注意:我的代码使用“Pylance”(就像 JS 中的 typescript)
答案 10 :(得分:-1)
使用get_fieldsets,您可以从表单
获取所有字段def get_readonly_fields(self, request, obj=None):
readonly = []
for fs in self.get_fieldsets(request, obj):
if len(fs) > 1:
readonly += fs[1].get('fields', [])
return readonly