按照官方文档说明自定义了user模块。
文件目录结构如下:
accounttest
--account
----__init__.py
----admin.py
----models.py
----tests.py
----views.py
--accounttest
----__init__.py
----settings.py
----urls.py
----wsgi.py
--manage.py
代码如下:
**models.py**
# coding=utf-8
from django.db import models
# Create your models here.
import uuid
from django.conf import settings
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser
class PassportManager(BaseUserManager):
def create_user(self,email,username,password=None):
if not email:
raise ValueError('Users must have a email address')
user = self.model(
email = PassportManager.normalize_email(email),
username = username,
uuid = str(uuid.uuid1()),
)
user.set_password(password)
user.uuid = str(uuid.uuid1())
user.save(using=self._db)
def create_superuser(self,email,username,password):
user = self.create_user(email,
username =username,
password = password,)
user.uuid = str(uuid.uuid1())
user.is_staff = True
user.is_active = True
user.is_admin = True
user.sace(using=self._db)
return user
class Passport(AbstractBaseUser):
uuid = models.CharField(max_length=36,unique=True, db_index=True)
username = models.CharField(max_length=40, unique=True, db_index=True)
email = models.EmailField(max_length=254)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
address = models.CharField(max_length=140,blank=True, null=True)
dep = models.CharField(max_length=30,blank=True, null=True)
role = models.CharField(max_length=30,blank=True, null=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
objects = PassportManager()
class Meta:
app_label = 'passport'
=====================
**settings.py**:
"""
Django settings for accounttest project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'm8n%teued!sq%)_6*%(vhz%7s&i6!0hdkwk#ecryi9u%y4r%v+'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
AUTH_USER_MODEL='account.Passport'
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounttest',
'account'
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'accounttest.urls'
WSGI_APPLICATION = 'accounttest.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'accounttest3', # Or path to database file if using sqlite3.
'USER': 'accounttest3', # Not used with sqlite3.
'PASSWORD': '123456', # Not used with sqlite3.
'HOST': '192.168.138.58', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
在执行python manage.py validate的时候报错了:
“”“
CommandError: One or more models did not validate:
admin.logentry: 'user' has a relation with model account.Passport, which has either not been installed or is abstract.
auth.user: Model has been swapped out for 'account.Passport' which has not been installed or is abstract.
”“”
请问如何解决啊