0

我正在尝试创建一个新用户,但收到以下消息:

_ get _ raise AttributeError("Manager 不可通过 %s 实例访问" % type._ name _) AttributeError: Manager 不可通过 CompanyUser 实例访问

这就是我正在做的事情:

  def obj_create(self, bundle, request=None, **kwargs):
    try:
        bundle = super(AccountCreateResource, self).obj_create(bundle)
        bundle.obj.set_password(bundle.data.get('password'))

        bundle.obj.objects.create_user()
    except IntegrityError:
        raise BadRequest('Username already exists')

我应该可以访问经理。这是我的模型:

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


class EmailUserManager(BaseUserManager):

    def create_user(self, email, password=None, **extra_fields):
        """
        Creates and saves an EmailUser with the given email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('The given email must be set')
        email = EmailUserManager.normalize_email(email)
        user = self.model(email=email, is_staff=False, is_active=True,
                          is_superuser=False, last_login=now,
                          date_joined=now, **extra_fields)

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(email, password, **extra_fields)
        user.is_staff = True
        user.is_active = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class AbstractEmailUser(AbstractBaseUser, PermissionsMixin):
    """
    Abstract User with the same behaviour as Django's default User but
    without a username field. Uses email as the USERNAME_FIELD for
    authentication.

    Use this if you need to extend EmailUser.

    Inherits from both the AbstractBaseUser and PermissionMixin.

    The following attributes are inherited from the superclasses:
        * password
        * last_login
        * is_superuser
    """
    email = models.EmailField(_('email address'), max_length=255,
                              unique=True, db_index=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 = EmailUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    class Meta:
        abstract = True

    def get_full_name(self):
        """
        Returns the email.
        """
        return self.email

    def get_short_name(self):
        """
        Returns the email.
        """
        return self.email

    def email_user(self, subject, message, from_email=None):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email])


class CompanyUser(AbstractEmailUser):
    """
    Concrete class of AbstractEmailUser.

    """

    company = models.CharField(max_length=100)


    pass
4

2 回答 2

2

您没有显示足够的代码或回溯以了解实际发生的情况,但无论如何:您收到的错误消息意味着您尝试从实例访问管理器,但这是不允许的,句号 - 您拥有的事实抽象模型是完全不相关的。您必须从模型类本身访问 Manager。希望这只是替换为的instance.objects问题type(instance).objects

于 2013-10-15T11:45:22.980 回答
1

真的不清楚你在这里想要做什么。这一行 - 导致错误的行 - 没有意义:

bundle.obj.objects.create_user()

由于您没有提供 的代码bundle,我不得不猜测这是一个带有objForeignKey 的模型,它指向CompanyUser(虽然命名约定相当奇怪)。但正如错误所说,您不能调用objects实例,只能调用类。但即使你可以,这条线仍然没有意义:obj已经是一个用户,那你为什么要创建一个?同样,create_user至少需要一个电子邮件参数,您没有提供该参数。

于 2013-10-15T11:46:18.143 回答