我需要在我的自定义用户模型中使用 user_id 而不是 id (不要问为什么,这是一个很长的故事:))。我尝试使用 django 示例中的代码,但是当我尝试登录管理后端时,我在模板 site-packages/django/contrib/admin/templates/admin/index.html,第 60 行“用户”处出现错误对象没有属性“id”
有错误的行看起来像
{% get_admin_log 10 as admin_log for_user user %}
#UserManager
class UserManager(BaseUserManager):
def create_user(self, login_name, email, first_name, last_name, password=None):
if not email:
raise ValueError('The given email must be set')
email = UserManager.normalize_email(email)
user = self.model(login_name=login_name, email=email,
first_name=first_name,
last_name=last_name,
is_staff=False, is_active=True, is_superuser=True)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, login_name, email, first_name, last_name, password):
u = self.create_user(login_name, email, first_name, last_name, password)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save(using=self._db)
return u
#Model
class User(AbstractBaseUser, PermissionsMixin):
user_id = models.AutoField(primary_key=True)
login_name = models.CharField(max_length=30, unique=True, db_index=True)
email = models.EmailField(max_length=255, unique=True)
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
phone = models.CharField(max_length=128)
objects = UserManager()
USERNAME_FIELD = 'login_name'
REQUIRED_FIELDS = ['email', 'first_name', 'last_name']
is_staff = models.BooleanField('staff status', default=False)
is_active = models.BooleanField('active', default=True)
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
#Admin
from django.contrib import admin
from applications.users.models import User
class UserAdmin(admin.ModelAdmin):
list_display = ('login_name', 'email', 'first_name', 'last_name')
admin.site.register(User, UserAdmin)
我该如何解决?