1

在我当前的项目中,我正在替换默认的 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)
                    }))
4

1 回答 1

5

app_label = 'djutils'在您的用户模型的Meta类中设置。

有关更多信息,请参阅app_label文档。

于 2013-06-02T18:02:27.053 回答