将为管理员创建配置文件的原因是您使用
create_user
在里面
create_superuser
。首先将保存一个普通用户。这里为每个人创建了配置文件。此用户将被两次修改为admin。你应该在你的
创建超级用户
功能:
def create_user(self, username=None, email=None, password=None, is_admin=False):
"""
Creates and saves a User with the given username, email and password.
"""
if not username:
raise ValueError('Must include username')
if not email:
raise ValueError('Users must have an email address')
user = self.model(
username = username,
email = self.normalize_email(email),
gender='MALE',
is_admin = is_admin,
)
user.set_password(password)
user.save(using=self._db)
print user
return user
def create_superuser(self, username, email, password):
"""
Creates and saves a superuser with the given username, email and password.
"""
user = self.create_user(
username=username,
email=email,
password=password,
is_admin = True,
)
return user
或
if instance.is_admin:
UserProfile.objects.filter(user=instance).delete()
之后
if not instance.is_admin
…但这种方式并不优雅