admin管理员组

文章数量:1123425

I create my custom user and group as below:

models.py (myusers app)

` from django.db import models from django.contrib.auth.models import AbstractUser, GroupManager, Permission

class AbstractGroup(models.Model):
    name = models.CharField(_("name"), max_length=150, unique=True)
    permissions = models.ManyToManyField(
        Permission,
        verbose_name=_("permissions"),
        blank=True,
        related_name="fk_group_permission",
    )

    objects = GroupManager()

    def __str__(self):
        return self.name

    def natural_key(self):
        return (self.name,)

    class Meta:
        verbose_name = _("group")
        verbose_name_plural = _("groups")
        abstract = True

class Group(AbstractGroup):
    is_active = models.BooleanField(_("Is Active"), default=True)

class User(AbstractUser):
    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 their groups."
        ),
        related_name="user_set",
        related_query_name="user",
    )

`

and point in settings.py: AUTH_USER_MODEL=myusers.User AUTH_GROUP_MODEL=myusers.Group

All is ok but when get group permissions (a_user.get_all_permissions() or a_user.get_group_permissions()) raise error: ValueError: Cannot query “a_user”: Must be “Group” instance. (a_user here is the user user instance) what is wrong?

The get_group_permissions() return error.

本文标签: Can not get group permissions by django custom User and GroupStack Overflow