現在のプロジェクトでは、デフォルトの django ユーザー モデルを置き換えています。モデルの数が増え始めたので、実装をいくつかのフォルダーに分割することにしましたが、django はユーザー モデルを見つけることができません。
私はこれを持っていました。
djutils
models.py
UserManager
User
これに変えたら動かなくなった
djutils
models
__init__.py
from djutils.models.user import UserManager, User
user.py
UserManager
User
まだモデルをインポートするべきではありませんか? 私は得る:
CommandError: One or more models did not validate:
auth.user: Model has been swapped out for 'djutils.User' which has not been installed or is abstract.
私のsettings.pyにこれがあります
AUTH_USER_MODEL = 'djutils.User'
(最初のケースが機能することに注意してください)
お知らせ下さい!よろしく
編集
djutils
models
__init__.py
from __future__ import absolute_import
from djutils.models.base import Manager, PrivateModel, Model
from djutils.models.user import UserManager, User
from djutils.models.passwordrecovery import PasswordRecoveryManager, PasswordRecovery
user.py
from djutils.models.base import Manager, Model
from djutils import mail
from djutils import settings
from django.contrib.auth.models import PermissionsMixin, AbstractBaseUser, BaseUserManager
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from djutils import fields
class UserManager(Manager, BaseUserManager):
def create_user_instance(self, email, password, first_name, last_name, **extra_fields):
return user
def create_user(self, email, password, first_name, last_name, **extra_fields):
return user
def create_superuser(self, email, password, first_name, last_name, **extra_fields):
return user
class User(Model(20), AbstractBaseUser, PermissionsMixin):
# Basico info
first_name = fields.NameField(max_length=20, help_text=_('First name'))
last_name = fields.NameField(max_length=20, help_text=_('Last name'))
email = fields.EmailField(max_length=255, unique=True, help_text=_('Email'))
url = fields.URLField(help_text=_('Personal homepage'))
bio = fields.TextField(help_text=_('Biographic details'))
#picture = fields.ImageField(help_text=_('Profile picture'))
# Permissions
is_active = fields.BooleanField(default=False, help_text=_('Active user'))
is_staff = fields.BooleanField(default=False, help_text=_('Staff member'))
# Logging
date_joined = fields.DateTimeField(default=timezone.now)
# A string describing the name of the field on the User model that is used as the unique identifier
USERNAME_FIELD = 'email'
# A list of the field names that must be provided when creating a user via the createsuperuser management command
REQUIRED_FIELDS = ['first_name', 'last_name', 'password']
# Fields that are not returned by get_public
PRIVATE = ['is_active', 'is_superuser', 'is_staff', 'last_login', 'groups', 'user_permissions', 'password', 'id']
# Model manager
objects = UserManager()
class Meta:
swappable = 'AUTH_USER_MODEL'
permissions = ()
def send_mail(self, request, subject, body, context={}):
mail.send_mail(request, subject, body, [self.email], context)
def send_confirmation_email(self, request, context={}):
self.send_mail(request, settings.CONFIRMATION_SUBJECT, settings.CONFIRMATION_TEMPLATE, dict(context, **{
'redirect': request.build_absolute_uri(settings.CONFIRMATION_REDIRECT_URL)
}))