获取用户分组信息
def get_group_names(self, user):
group_names = []
for g in user.groups.all():
group_names.append(g.name)
return group_names
自定义 fieldsets
def get_fieldsets(self, request, obj=None):
group_names = self.get_group_names(request.user)
if 'interviewer' in group_names and obj.first_interviewer_user == request.user:
return self.default_fieldsets_first
if 'interviewer' in group_names and obj.second_interviewer_user == request.user:
return self.default_fieldsets_second
return self.default_fieldsets
自定义 readonly_fields
def get_readonly_fields(self, request, obj):
group_names = self.get_group_names(request.user)
if 'interviewer' in group_names:
return 'first_interviewer_user', 'second_interviewer_user'
return ()