如何让我的CustomUser出现在auth app下的admin中,就像内置用户一样?我知道有一个像here这样的问题,我遵循了人们建议的解决方案,但是他们的解决方案做的是,它在我的应用程序中创建了一个客户用户,而不是在auth应用程序中,所以像任何其他模型一样我创造。
以下是我的模特:
class CustomUserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None,
**extra_fields):
'''
Create a CustomUser with email, name, password and other extra fields
'''
now = timezone.now()
if not email:
raise ValueError('The email is required to create this user')
email = CustomUserManager.normalize_email(email)
cuser = self.model(email=email, first_name=first_name,
last_name=last_name, is_staff=False,
is_active=True, is_superuser=False,
date_joined=now, last_login=now,)
cuser.set_password(password)
cuser.save(using=self._db)
return cuser
def create_superuser(self, email, first_name, last_name, password=None,
**extra_fields):
u = self.create_user(email, first_name, last_name, password,
**extra_fields)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save(using=self._db)
return u
class CustomUser(AbstractBaseUser, PermissionsMixin):
'''
Class implementing a custom user model. Includes basic django admin
permissions and can be used as a skeleton for other models.
Email is the unique identifier. Email, password and name are required
'''
email = models.EmailField(_('email'), max_length=254, unique=True,
validators=[validators.validate_email])
username = models.CharField(_('username'), max_length=30, blank=True)
first_name = models.CharField(_('first name'), max_length=45)
last_name = models.CharField(_('last name'), max_length=45)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Determines if user can access the admin site'))
is_active = models.BooleanField(_('active'), default=True)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
def get_full_name(self):
'''
Returns the user's full name. This is the first name + last name
'''
full_name = "%s %s" % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
'''
Returns a short name for the user. This will just be the first name
'''
return self.first_name.strip()
我还想将ManyToManyField
添加到我拥有的其他2个模型中,并将它们显示在admin中的用户表单中。
这是否意味着我必须写自己的表格?或者我可以只复制内置用户表单的源代码并将其更改为我的名字?
提前多多感谢!
答案 0 :(得分:1)
为什么在auth app中需要它?为什么这么重要?如果你真的需要这样做,你可以在`Meta
中添加一个app_label
变量
class Meta:
app_label = 'auth'
这会更改表名,因此您需要迁移这些名称。
对于ManyToManyField
,我只需覆盖相应的身份验证表单并添加这些字段。