django自定义用户模型引用组和权限

时间:2014-09-17 13:04:30

标签: python django inheritance

我制作了自定义用户模型..

class User(AbstractBaseUser, PermissionsMixin):

    username = models.CharField(_('username'), max_length=30, unique=True,
    help_text=_('Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters'),
    validators=[
        validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), _('invalid'))
    ])

    first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)

    last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)

    email = models.EmailField(_('email address'), max_length=255)

    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=False,
        help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))

    '''
    groups = models.ManyToManyField(Group, verbose_name=_('groups'),
        blank=True, help_text=_('The groups this user belongs to. A user will '
        'get all permissions granted to each of '
        'his/her group.'),
        related_name="tmp_user_set", related_query_name="user")

    user_permissions = models.ManyToManyField(Permission,
        verbose_name=_('user permissions'), blank=True,
        help_text=_('Specific permissions for this user.'),
        related_name="tmp_user_set", related_query_name="user")
    '''



    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

    objects = UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email',]

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        return self.first_name

    def email_user(self, subject, message, from_email=None):
        send_mail(subject, message, from_email, [self.email])

当我将组引用到Group和user_permissions到Permission时,它会给出错误。从管理员端添加这些组和权限时,如何获取这些组和权限。

当我创建像医院这样的另一个模型并从像医院(用户)这样的用户继承它时,我可以获得医院用户的字段。

任何帮助??

0 个答案:

没有答案