我正在与Django及其管理页面合作。我有几个属性的类。我创建了一个组,其中该组的用户有权添加,更改和删除这些类的属性的多个内容。现在类中还有一些属性,这些属性与该组的用户无关,但对于SuperUser,为什么我不会在模型中删除它们。我想隐藏几个字段给这个组的用户,因为我可以设置更改,添加和删除类的每个属性而不是类的几个属性。有可能这样做,什么时候可以,怎么做?非常感谢帮助,如果您有任何疑问,请问我。
编辑:
from django.contrib import admin
from programm.models import completedTask
class completedTasks(admin.ModelAdmin):
fields = ["date", "duration"]
def change_view(self, request, object_id, extra_context=None):
restricted_groups = set(["Azubi"])
extend_list = ["documentationReference", "educationDepartment"]
if not set(request.user.groups.values_list("name", flat=True)).intersection(restricted_groups):
self.fields.extend(extend_list)
return super(completedTasks, self).change_view(request, object_id, extra_context=extra_context)
admin.site.register(completedTask, completedTasks)
答案 0 :(得分:1)
以下可能适合您,但我没有测试它。
字段集必须是list
。不能使用tuple
和其他不可编辑的类型,因为您将在运行时编辑它们的内容。将所有字段添加到字段集中,包括您要为某些组限制的属性
restricted_groups = set() # you must either define this statically or fill it on runtime. But it must be a set
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
('custom', {'fields': ['question_text', 'item1', 'item2']}),
('Date information', {'fields': ['pub_date']}),
]
def change_view(self, request, object_id, extra_context=None):
if set(request.user.groups.values_list("name", flat=True)).intersection(restricted_groups): # If there is same group(s) in both the users groups and the restricted groups, then user will not see following fields
filter(lambda x: x[0]=="custom", self.fieldsets)[0][1]["fields"].remove("item1")
filter(lambda x: x[0]=="custom", self.fieldsets)[0][1]["fields"].remove("item1")
return super(QuestionAdmin, self).change_view(request, object_id, extra_context=extra_context)
你也可以对add_view
使用同样的东西。这是doc
更新,request.user
组过滤已修复并设置为使用组名。如果您希望使用其他属性(例如,组ID),则必须更改它
注意:该代码未经过测试,但我根据其编写(略有不同)。您最好对其进行测试,并检查它是否适用于受限制的组和不受限制的组。
更新2:很明显,如果您坚持使用fieldsets
,这不是最好的方法,因为它的结构不太适合这种方法。该示例基于您为每个类别(在我的示例中为custom
)提供唯一名称,并且每个fields
属性包含字段列表,而不是用于对其进行子分类的元组。
以下是另一种方法,如果用户位于不在 restricted_groups
中,则添加这些字段:
class completedTasks(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['date']}),
(None, {'fields': ['duration']}),
(None, {'fields': ['documentationReference']}),
("Extra", {'fields': []}),
]
def change_view(self, request, object_id, extra_context=None):
if not set(request.user.groups.values_list("name", flat=True)).intersection(restricted_groups): # there is no restricted group in users groups
filter(lambda x: x[0]=="Extra", self.fieldsets)[0][1]["fields"].extend(["field1", "field2"]) # add fields to fieldsets. extend must accept a list of fields to be added to the admin
return super(completedTasks, self)..change_view(request, object_id, extra_context=extra_context)
更新: Sinmce你只有一个组,你删除了set的用法。我在我的项目中使用changelist_view
进行此类提议,我将其添加到代码中
class completedTasks(admin.ModelAdmin):
fields = ["date", "duration"]
def change_view(self, request, object_id, extra_context=None):
if "Azubi" not request.user.groups.values_list("name", flat=True):
if "field1" not in self.fields:
self.fields.append("field1")
else:
if "field1" in self.fields:
self.fields.remove("field1")
return super(completedTasks, self).change_view(request, object_id, extra_context=extra_context)
def changelist_view(self, request, extra_context=None):
if "Azubi" not request.user.groups.values_list("name", flat=True):
if "field1" not in self.fields:
self.fields.append("field1")
else:
if "field1" in self.fields:
self.fields.remove("field1")
return super(completedTasks, self).changelist_view(request, extra_context=None)