抽象基础管理器无法访问

时间:2013-10-15 11:16:02

标签: python django django-models

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

_ GET 的_     引发AttributeError(“管理器无法通过%s实例访问”%type ._ name _) AttributeError:无法通过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

2 个答案:

答案 0 :(得分:2)

您没有显示足够的代码或回溯来了解实际发生了什么,但无论如何:您获得的错误消息意味着您尝试从实例访问Manager,并且不允许,期间 - 事实你有一个抽象的模型是完全无关紧要的。您必须从模型类本身访问Manager。希望只需将instance.objects替换为type(instance).objects

答案 1 :(得分:1)

目前还不清楚你在这里要做什么。这一行 - 导致错误的那一行 - 毫无意义:

bundle.obj.objects.create_user()

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