Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建Django自定义用户模型时出现关系错误_Python_Django_Django Models - Fatal编程技术网

Python 创建Django自定义用户模型时出现关系错误

Python 创建Django自定义用户模型时出现关系错误,python,django,django-models,Python,Django,Django Models,我一直在关注如何在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: A

我一直在关注如何在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'.
我理解存在多对多关系问题,我认为这是由
许可证smixin
引起的。但我不是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冻结

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\u USER\u MODEL='member.CustomUser'
设置为
AUTH\u USER\u MODEL='AUTH.USER'


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

您必须在settings.py上声明
AUTH\u USER\u MODEL
。就你而言:

AUTH_USER_MODEL = 'member.customuser'

我想说AUTH_USER_模型是这里的问题,它应该明确指向您的CustomUser,这将阻止创建AUTH.USER。我有一个API在生产环境中运行,因此,理想情况下,我希望能够迁移到新的用户模型,而不删除任何记录。是否没有办法将所有这些迁移到新的用户模型?如果我真的不得不放弃它,这还不是世界末日,但我宁愿不放弃。我不知道你的评论和我的回复有什么关系。由于您链接到州的迁移文档,只要您的外键指向AUTH_USER_MODEL,您就可以了。@DanielGroves这完全是另一个问题:)。IMHO您有一些选项,比如转储用户表并将其重新加载到新表中,或者尝试使用South(使用与默认身份验证用户相同的字段创建用户、迁移、更改字段、再次迁移…)。我会使用dump/reload方法:)