如何使我的CustomUser像内置用户一样在auth应用下的admin中显示?我知道存在这样的问题,我遵循了人们建议的解决方案,但是他们的解决方案做了什么,那就是它使我的应用程序(而不是身份验证应用程序)中的客户用户成为了用户,因此就像我创建的任何其他模型一样。
这是我的模型:
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的用户表单中。这是否意味着我必须编写自己的表格?或者,也许我可以只复制内置用户表单的源代码并将其更改为我的名字?
在此先多谢!
最佳答案
为什么在auth应用中需要它?为什么这么重要?如果确实需要这样做,则只需在`Meta中添加一个app_label
变量
class Meta:
app_label = 'auth'
这将更改表名,因此您将需要迁移这些表名。
对于
ManyToManyField
,我将覆盖适当的身份验证表单并添加这些字段。关于django - Django:让CustomUser出现在Auth下的admin中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20443049/