"""将指定手机号设为超级管理员(功能权限 + 集团视图)。""" from django.core.management.base import BaseCommand from django.db import transaction from jituan.constants import SUPER_ADMIN_PHONES from users.business_models import User class Command(BaseCommand): help = '将 SUPER_ADMIN_PHONES 中的账号设为 IsSuperuser + UserType=admin' def handle(self, *args, **options): updated = 0 for phone in SUPER_ADMIN_PHONES: users = list(User.query.filter(Phone=phone)) if not users: self.stdout.write(self.style.WARNING(f'未找到手机号 {phone}')) continue for user in users: with transaction.atomic(): user.IsSuperuser = True user.IsStaff = True if hasattr(user, 'UserType'): # UserType 为 property 从角色推断,尽量写 gvsdsdk 角色 pass user.save(update_fields=['IsSuperuser', 'IsStaff']) updated += 1 self.stdout.write(self.style.SUCCESS( f'已设置超管: {phone} (UserUID={user.UserUID})' )) self.stdout.write(self.style.SUCCESS(f'完成,更新 {updated} 个账号'))