gpt4 book ai didi

django - 扩展 django 用户模型和错误

转载 作者:行者123 更新时间:2023-12-02 07:25:16 24 4
gpt4 key购买 nike

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.pysyncdb (或其他任何东西)时,我得到这个:

(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

我只需要向默认用户模型添加一些字段(不希望新创建一个全新的身份验证类子类 AbstractBaseUser)

那么我做错了什么?

最佳答案

解决方案

  1. 要删除,重新创建数据库
  2. 删除 *.pyc 文件
  3. 删除迁移文件夹

然后 python manage.py makemigrations 完美运行。

关于django - 扩展 django 用户模型和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com