方法一:繼承AbstractUser 不過須要在setting中指出AUTH_USER_MODEL 該方法是 在原有的默認user表上增長字段 class suhao_user(AbstractUser): age = models.IntegerField(default=18) 方法二:繼承AbstractBaseUser,原有3個字段 password,last_login,is_active from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin from django.db import models from django.utils.translation import ugettext_lazy as _ class suhao_user_manage(BaseUserManager): def create_user(self, username, password, **extra_fields): user = self.model(username=username, is_staff=True, is_superuser=False, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, username, password, **extra_fields): user = self.model(username=username, is_staff=True, is_superuser=True, **extra_fields) user.set_password(password) user.save(using=self._db) return user class suhao_user(AbstractBaseUser, PermissionsMixin): username= models.CharField(verbose_name=u"用戶暱稱", max_length=30, unique=True) age = models.IntegerField(default=0) is_staff = models.BooleanField(_('staff status'), default=False) USERNAME_FIELD = 'username' objects = suhao_user_manage() def get_full_name(self): return self.username def get_short_name(self): return self.username ## 其中is_staff是必須字段,下面的2個方法也必定要重寫 方法3:使用OneToOneField class suhao_info(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) age = models.IntegerField(default=18) .... django 用戶認證系統 authenticate()--》login()--》logout() 其中authenticate()能夠本身寫 在setting中配置AUTHENTICATION_BACKENDS from django.contrib.auth.decorators import login_required login_required 若是用戶沒有登入,則重定向到settings.LOGIN_URL,並傳遞當前查詢字符串中的絕對路徑。例如:/accounts/login/?next=/polls/3/。 login_required 可選參數redirect_field_name, login_url, user_passes_test 可選參數redirect_field_name, login_url,