子模型中的Django用户模型

时间:2013-06-02 17:07:03

标签: python django django-models python-3.x django-authentication

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

1 个答案:

答案 0 :(得分:4)

在用户模型的app_label = 'djutils'课程中设置Meta

有关详细信息,请参阅app_label文档。