【发布时间】:2015-09-21 14:33:36
【问题描述】:
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
在 settings.py 我添加了应用程序路径: APPS_PATH = os.path.join(BASE_DIR, "apps") 到 sys.path,
当我运行 python manage.py syncdb(或其他任何东西)时,我得到了这个:
(cmr) F:\_Projects\cmr\containers\backend>python manage.py syncdb
F:\_Projects\cmr\.venv\cmr\lib\importlib\_bootstrap.py:321: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
return f(*args, **kwds)
SystemCheckError: System check identified some issues:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'AppUser.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'AppUser.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'AppUser.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'AppUser.user_permissions'.
consents.AppUser.groups: (fields.E304) Reverse accessor for 'AppUser.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'AppUser.groups' or 'User.groups'.
consents.AppUser.user_permissions: (fields.E304) Reverse accessor for 'AppUser.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'AppUser.user_permissions' or 'User.user_permissions'.
如果我在设置中取消注释此行
# AUTH_USER_MODEL = 'consents.AppUser'
我收到另一个错误:
ValueError: Dependency on unknown app: consents
我只需要向默认用户模型添加几个字段(不希望新创建一个全新的身份验证类 subclassign AbstractBaseUser)
那我做错了什么?
【问题讨论】:
-
你真的需要继承
AbstractUser而不是AbstractBaseUser吗? -
是的,这是更快的方式。我没有更改身份验证,只是将字段添加到用户模型。我不喜欢 'profile' 解决方案,因为我需要与 djoser 包集成
-
好吧,你应该明确地取消注释 settings.py 中告诉 Django 使用你的自定义用户的行,这就是触发巨大错误的原因。现在让我们处理另一个......问题是您有两个基于相同抽象模型的具体模型,并且外键或多对多字段在它们的
related_name中发生冲突。 -
试试:
AUTH_USER_MODEL = 'apps.consents.AppUser' -
它不起作用,它只允许 single 。在价值上。所以我不得不将应用程序的路径添加到 sys.path
标签: django django-models django-authentication