获取用户分组信息

  1. def get_group_names(self, user):
  2. group_names = []
  3. for g in user.groups.all():
  4. group_names.append(g.name)
  5. return group_names

自定义 fieldsets

  1. def get_fieldsets(self, request, obj=None):
  2. group_names = self.get_group_names(request.user)
  3. if 'interviewer' in group_names and obj.first_interviewer_user == request.user:
  4. return self.default_fieldsets_first
  5. if 'interviewer' in group_names and obj.second_interviewer_user == request.user:
  6. return self.default_fieldsets_second
  7. return self.default_fieldsets

自定义 readonly_fields

  1. def get_readonly_fields(self, request, obj):
  2. group_names = self.get_group_names(request.user)
  3. if 'interviewer' in group_names:
  4. return 'first_interviewer_user', 'second_interviewer_user'
  5. return ()