Python Django admin - 将所有字段设为只读
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13817525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django admin - make all fields readonly
提问by yprez
I'm trying to make all fields readonly without listing them explicitly.
我试图将所有字段设为只读而不明确列出它们。
Something like:
就像是:
class CustomAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
if request.user.is_superuser:
return self.readonly_fields
return self.fields
The problem is CustomAdmin.fieldsis not set at this point.
问题是CustomAdmin.fields在这一点上没有设置。
Any ideas?
有任何想法吗?
采纳答案by Danny W. Adair
Careful, self.model._meta.fields are not necessarily the same fields that CustomAdmin has!
小心,self.model._meta.fields 不一定是 CustomAdmin 拥有的字段!
"All fields of the Admin" would look more like this:
“管理员的所有字段”看起来更像这样:
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]
))
回答by Hedde van der Heide
You could iterate through the model meta fields:
您可以遍历模型元字段:
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
回答by yprez
Ok, now there's this:
好的,现在有这个:
class CustomAdmin(admin.ModelAdmin):
def get_readonly_fields(self, request, obj=None):
# ...
return [f.name for f in self.model._meta.fields]
Still looking for a less ugly way.
仍在寻找一种不那么丑陋的方式。
回答by pgranger
With get_fieldsets you get all fields from the form
使用 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
回答by Ohad the Lad
For Inlines (Tab or Stack)
对于内联(选项卡或堆栈)
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
回答by user2111922
This worked for me with Django 1.10
这在 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]
))
回答by Arindam Roychowdhury
My requirement was similar . I needed only onefield to be shown as read-only . And this worked fine:
我的要求是相似的。我只需要一个字段显示为 read-only 。这工作正常:
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]
Refer: C:\Python27\Lib\site-packages\django\contrib\admin\options.py
参考:C:\Python27\Lib\site-packages\django\contrib\admin\options.py
回答by ????? ???????
Since django 2.1, you can prevent editing, while allowing viewing, by returning Falsefrom the ModelAdmin's has_change_permissionmethod, like this:
从 django 2.1 开始,您可以通过False从ModelAdmin的has_change_permission方法返回来阻止编辑,同时允许查看,如下所示:
class CustomAdmin(admin.ModelAdmin):
def has_change_permission(self, request, obj=None):
return False
(This will not work before django 2.1, as it will also deny permission to any user trying only to view.)
(这在 django 2.1 之前不起作用,因为它也会拒绝任何仅尝试查看的用户的权限。)
回答by ali Saen
@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
...
readonly_fields = ["father", "mother", "spouse"]
reference : https://books.agiliq.com/projects/django-admin-cookbook/en/latest/changeview_readonly.html
参考:https: //books.agiliq.com/projects/django-admin-cookbook/en/latest/changeview_readonly.html

