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 にアクセスしようとしたことを意味し、それは許可されていません。抽象的なモデルはまったく関係ありません。モデル クラス自体から Manager にアクセスする必要があります。うまくいけば、それは に置き換えるだけの問題instance.objectsですtype(instance).objects

于 2013-10-15T11:45:22.980 に答える