创建无密码超级用户



我已经实现了自定义用户如下:

class UserManager(BaseUserManager):
def _create_user(self, email, username, shop, number, is_superuser, total_points, used_points, is_staff, **extra_fields):
if not username and (is_superuser or not number or not shop):
raise ValueError('Users must have a username')
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
username=username,
email=email,
shop=shop,
number=number,
is_superuser=is_superuser,
total_points=total_points,
used_points=used_points,
last_login=now,
date_joined=now,
is_staff=is_staff,
**extra_fields
)
user.save(using=self._db)
return user
def get_by_natural_key(self, username):
return self.get(**{'{}__iexact'.format(self.model.USERNAME_FIELD): username})
def create_user(self, email, username, shop, number,  is_superuser, total_points, used_points, is_staff, **extra_fields):
return self._create_user(email, username, shop, number, is_superuser, total_points, used_points, is_staff, **extra_fields)
def create_superuser(self, email, username, password=None):
user = self.model(email=email, username=username,  shop=None, is_superuser=True, total_points=0, used_points=0,
is_staff=True)
user.is_superuser = True
user.is_staff = True
user.shop = Shop.objects.create(name="openformat")
user.save(using=self._db)
return user

class AbstractUser(AbstractBaseUser):
uid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
username = models.CharField(max_length=64, unique=True)
email = models.EmailField()
number = models.CharField(max_length=20)
is_superuser = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(null=True, blank=True)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'username'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['email']
class Meta:
abstract = True

class User(AbstractUser, PermissionsMixin):
shop = models.ForeignKey(Shop, null=False, blank=False, on_delete=models.CASCADE)
total_points = models.IntegerField(default=0)
used_points = models.IntegerField(default=0)
objects = UserManager()
def get_absolute_url(self):
return "/users/%i/" % (self.uid)
def has_perm(self, perm, obj=None):
return self.is_superuser
def get_full_name(self):
return self.username
def get_short_name(self):
return self.username
def __str__(self):
return self.username

如您所见,我没有自定义用户的密码字段。我不希望管理员也有密码字段。

问题
  1. createsuperuser仍在询问密码
  2. 因为我在create_superuser功能中创建了一个没有密码的超级用户,我无法登录管理面板(密码不匹配)

有没有办法让超级用户没有密码?

是否可以在User模型中拥有password字段,并且除了admin之外的所有用户都为空?

我是Django的新手。所以,这可能是一个非常"愚蠢"的问题。问题。

password是必须的,如果你想使用django auth,如果你不想,我会建议一个坏主意,那就是重写django authhttps://stackoverflow.com/a/27682268/9501611

相关内容

  • 没有找到相关文章

最新更新