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 username not user instance)
what is wrong?
Error trace:
Traceback (most recent call last):
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 518, in thread_handler
raise exc_info[1]
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/core/handlers/exception.py”, line 42, in inner
response = await get_response(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 518, in thread_handler
raise exc_info[1]
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/core/handlers/base.py”, line 253, in _get_response_async
response = await wrapped_callback(
^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 468, in call
ret = await asyncio.shield(exec_coro)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/current_thread_executor.py”, line 40, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 522, in thread_handler
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/utils/decorators.py”, line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/mahstudy/helper/decorators.py”, line 11, in wrap
return func(request, *args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/decorators.py”, line 60, in view_wrapper
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/base.py”, line 143, in dispatch
return handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/detail.py”, line 109, in get
context = self.get_context_data(object=self.object)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/mahstudy/users/views.py”, line 171, in get_context_data
context[‘appspers’] = sorted({item.replace(“.”, ": “).replace(”", " ") for item in user.get_group_permissions()})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/models.py”, line 294, in get_group_permissions
return _user_get_permissions(self, obj, “group”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/models.py”, line 209, in _user_get_permissions
permissions.update(getattr(backend, name)(user, obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 99, in get_group_permissions
return self._get_permissions(user_obj, obj, “group”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 80, in _get_permissions
perms = getattr(self, “get%s_permissions” % from_name)(user_obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 64, in _get_group_permissions
return Permission.objects.filter({user_groups_query: user_obj})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/manager.py”, line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1476, in filter
return self._filter_or_exclude(False, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1494, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1501, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1609, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1641, in _add_q
child_clause, needed_inner = self.build_filter(
^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1528, in build_filter
self.check_related_objects(join_info.final_field, value, join_info.opts)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1342, in check_related_objects
self.check_query_object_type(value, opts, field)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1319, in check_query_object_type
raise ValueError(
ValueError: Cannot query “user”: Must be “Group” instance.
Internal Server Error: /token/yscFY1Yc8DT3o6CdP6hO-yQU9mFiAVIU7B6kei8Qs54
Traceback (most recent call last):
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 518, in thread_handler
raise exc_info[1]
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/core/handlers/exception.py”, line 42, in inner
response = await get_response(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 518, in thread_handler
raise exc_info[1]
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/core/handlers/base.py”, line 253, in _get_response_async
response = await wrapped_callback(
^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 468, in call
ret = await asyncio.shield(exec_coro)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/current_thread_executor.py”, line 40, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/asgiref/sync.py”, line 522, in thread_handler
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/utils/decorators.py”, line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/mahstudy/helper/decorators.py”, line 11, in wrap
return func(request, *args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/decorators.py”, line 60, in view_wrapper
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/base.py”, line 143, in dispatch
return handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/views/generic/detail.py”, line 109, in get
context = self.get_context_data(object=self.object)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/mahstudy/users/views.py”, line 171, in get_context_data
context[‘appspers’] = sorted({item.replace(“.”, ": “).replace(”", " ") for item in user.get_group_permissions()})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/models.py”, line 294, in get_group_permissions
return _user_get_permissions(self, obj, “group”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/models.py”, line 209, in _user_get_permissions
permissions.update(getattr(backend, name)(user, obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 99, in get_group_permissions
return self._get_permissions(user_obj, obj, “group”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 80, in _get_permissions
perms = getattr(self, “get%s_permissions” % from_name)(user_obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/contrib/auth/backends.py”, line 64, in _get_group_permissions
return Permission.objects.filter({user_groups_query: user_obj})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/manager.py”, line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1476, in filter
return self._filter_or_exclude(False, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1494, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/query.py”, line 1501, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1609, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1641, in _add_q
child_clause, needed_inner = self.build_filter(
^^^^^^^^^^^^^^^^^^
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1528, in build_filter
self.check_related_objects(join_info.final_field, value, join_info.opts)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1342, in check_related_objects
self.check_query_object_type(value, opts, field)
File “/home/admin/.virtualenvs/djangodev/lib/python3.11/site-packages/django/db/models/sql/query.py”, line 1319, in check_query_object_type
raise ValueError(
ValueError: Cannot query “user”: Must be “Group” instance.