Python Django自定义用户添加第二个用户模型

Python Django自定义用户添加第二个用户模型,python,django,django-models,Python,Django,Django Models,我目前正在使用Django 1.5自定义用户模型。使用下面的代码,如何添加新的第二类用户 我想添加一个名为StandardUser的新用户,我已经有了CompanyUser类型,即 class StandardUser(AbstractEmailUser): class Meta: app_label = 'accounts' 但这似乎不起作用,我如何才能做到这一点 下面的当前代码: class AbstractEmailUser(AbstractBaseUser, P

我目前正在使用Django 1.5自定义用户模型。使用下面的代码,如何添加新的第二类用户

我想添加一个名为
StandardUser
的新用户,我已经有了
CompanyUser
类型,即

class StandardUser(AbstractEmailUser):
    class Meta:
        app_label = 'accounts'
但这似乎不起作用,我如何才能做到这一点

下面的当前代码:

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

class CompanyUser(AbstractEmailUser):
    company = models.CharField(max_length=100)

    class Meta:
        app_label = 'accounts'

您的项目中只能有一个“官方”用户模型:

我建议你这样组织:

class StandardUser(AbstractEmailUser):
    class Meta:
        app_label = 'accounts'

class CompanyUser(StandardUser):
    company = models.CharField(max_length=100)

    class Meta:
        app_label = 'accounts'
在settings.py中

AUTH_USER_MODEL = 'myapp.StandardUser'
换句话说,根据Django模型继承,每个
公司用户
都有一个通过自动
OneToOneField
关联的
StandardUser

这种方法类似于Django,我认为这可能是唯一一种在Django中有效的方法

这意味着要查询非公司用户,您必须执行以下操作:

StandardUser.objects.filter(companyuser=None)

(您可能需要对此进行详细说明

也许如果你走这条路,
AbstractEmailUser
类就不再需要了,你可以重命名它,让它成为你具体的
StandardUser