扩展django用户模型及其错误

扩展django用户模型及其错误,django,django-models,django-authentication,Django,Django Models,Django Authentication,django 1.8.2 这是我的型号: class AppUser(AbstractUser): _SEX = ( ('M', 'Male'), ('F', 'Female'), ) _pregex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 d

django 1.8.2

这是我的型号:

class AppUser(AbstractUser):
    _SEX = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    _pregex  = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone    = models.CharField(validators=[_pregex], max_length=16, blank=True)
    gender   = models.CharField(max_length=1, blank=True, choices=_SEX)
    birthday = models.DateField(blank=True)
    vericode = models.CharField(max_length=40, blank=True) # verification code over SMS?
    verified = models.DateTimeField(null=True) # datetime stored when verification happened

    @property
    def age(self):
        today = date.today()
        return today.year - self.birthday.year - ((today.month, today.day) < (self.birthday.month, self.birthday.day))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# register externals
EXTERNAL_LIBS_PATH = os.path.join(BASE_DIR, "_externals", "libs")
EXTERNAL_APPS_PATH = os.path.join(BASE_DIR, "_externals", "apps")
APPS_PATH = os.path.join(BASE_DIR, "apps")
sys.path = ["", EXTERNAL_APPS_PATH, EXTERNAL_LIBS_PATH, APPS_PATH] + sys.path

# TEST PATH
TEST_ASSETS = os.path.join(BASE_DIR, "_test")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = (
    'suit',
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'cacheops',
    'rest_framework',
    'rest_framework.authtoken',
    'corsheaders',
    'djoser',
    'consents'
)

# Custom model for Auth
# AUTH_USER_MODEL = 'consents.AppUser'
我的文件夹结构是这样的

app
  -settings.py etc
apps
  -consents
# AUTH_USER_MODEL = 'consents.AppUser'
在settings.py中,我添加了应用程序路径: APPS\u PATH=os.PATH.join(BASE\u DIR,“APPS”)到sys.PATH

当我运行python manage.py syncdb(或其他任何东西)时,我得到以下结果:

如果我在设置中取消对此行的注释

app
  -settings.py etc
apps
  -consents
# AUTH_USER_MODEL = 'consents.AppUser'
我得到另一个错误:

ValueError: Dependency on unknown app: consents
我只需要在默认用户模型中添加几个字段(不希望创建新的auth类子类AbstractBaseUser)


那么我做错了什么呢?解决方案

  • 要删除、重新创建数据库
  • 删除*.pyc文件
  • 删除迁移文件夹

  • 然后python manage.py makemigrations工作得很好。

    您真的需要从
    AbstractUser
    而不是
    AbstractBaseUser
    继承吗?是的,这是一种更快的方法。我没有更改身份验证,只是向用户模型添加字段。我不喜欢“profile”解决方案,因为我需要与djoser Package集成。嗯,您应该明确地取消对settings.py中告诉Django使用自定义用户的行的注释,这就是触发巨大错误的原因。现在让我们来处理另一个。。。。问题是,您有两个基于同一抽象模型的具体模型,并且外键或多对多字段在其
    相关的\u名称中发生冲突
    。请尝试:
    AUTH\u USER\u model='apps.approvals.AppUser'
    它不起作用,它只允许一个。在价值上。因此,我不得不将应用程序的路径添加到sys.path。我也遇到了同样的问题。我只是想这就是解决办法。很高兴它对你有用,现在我也要试着去做。