gpt4 book ai didi

python - 创建 Django 自定义用户模型时的关系错误

转载 作者:行者123 更新时间:2023-11-28 17:43:41 25 4
gpt4 key购买 nike

我一直在关注this guide在 Django 中创建自定义用户模型。我是 Django 作为框架的新手,在尝试执行第八步时遇到了一系列四个错误,其中 south 用于构建迁移。

错误是:

auth.user: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
auth.user: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.
member.customuser: Accessor for m2m field 'groups' clashes with related m2m field 'Group.user_set'. Add a related_name argument to the definition for 'groups'.
member.customuser: Accessor for m2m field 'user_permissions' clashes with related m2m field 'Permission.user_set'. Add a related_name argument to the definition for 'user_permissions'.

我了解多对多关系问题,我认为这是由 PermissionsMixin 引起的。不过,我不是 100% 确定这一点。

这是我的自定义模型:

class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), max_length=254, unique=True)
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)

is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site'))
is_active = models.BooleanField(_('active'), default=True,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))

date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

objects = CustomUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []

class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')

def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()

def get_short_name(self):
return self.first_name

def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])

自定义用户管理器:

class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()

if not email:
raise ValueError('The given email must be set')

email = self.normalize_email(email)
user = self.model(email=email, is_staff=is_staff, is_active=True, is_superuser=is_superuser, last_login=now, date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password=None, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)

Models.py 导入:

from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

作为教程的一部分,我还创建了自定义表单和自定义管理位,但不认为它们与此问题相关。如果需要,我非常乐意将它们包括在内。

点卡住:

pip freeze
Django==1.6.1
South==0.8.4
argparse==1.2.1
coverage==3.7.1
distribute==0.6.24
django-form-utils==1.0.1
djangorestframework==2.3.10
psycopg2==2.5.1
wsgiref==0.1.2

Settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework.authtoken',
'rest_framework',
'south',
'log_api',
'member',
)

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 = 'server_observer.urls'

WSGI_APPLICATION = 'server_observer.wsgi.application'

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-GB'

TIME_ZONE = 'Europe/London'

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/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

# Where to look for templates

TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)

# Custom user model

#AUTH_USER_MODEL = 'member.CustomUser'
AUTH_USER_MODEL = 'auth.User'

# 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 = ''

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'server_observer',
'USER': '',
'PASSWORD': '',
'HOST': '127.0.0.1'
}
}

我也尝试将 AUTH_USER_MODEL = 'member.CustomUser' 设置为 AUTH_USER_MODEL = 'auth.User'

我被卡住了,昨晚已经在这上面花了很长时间了。如果有任何建议,我将不胜感激。

最佳答案

您必须在您的 settings.py 中声明 AUTH_USER_MODEL。在你的情况下:

AUTH_USER_MODEL = 'member.customuser'

关于python - 创建 Django 自定义用户模型时的关系错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20992564/

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