'django - method only works after the first call when using multi table inheritance

I'm trying to extend the Group and Permission models of django.contrib.auth.models to have some extra functionality. This is what I've done so far:

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, ContentType, Permission as Auth_Permission, Group as Auth_Group

class Group(Auth_Group):
    # Inherited attributes from django.contrib.auth.Group
    # name = models.Charfield()

    code = models.CharField(max_length=40, unique=True)

    group = models.OneToOneField(Auth_Group,
        on_delete=models.CASCADE,
        parent_link=True,
        related_name="group_extra"
    )

    description = models.CharField(max_length=400, null=True, blank=True)

    def __unicode__(self):
        return self.code

class Account(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True)

    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)

    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",
    )

I'm trying to override the representation of Group in the PermissionsMixin Model, so that when I call, for instance, first_group = <Account>.groups.all()[0], it returns the object as my Group class and I can then call first_group.code (This seems to be working fine). The problem lies when I try to call <Account>.get_all_permissions()... This is what happens:

>>> fmt = Account.objects.all()[0]
>>> fmt.get_all_permissions()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\projects\Project\App\authentication\models.py", line 158, in get_all_permissions
    return _user_get_all_permissions(self, obj)
  File "C:\projects\python3-venv\lib\site-packages\django\contrib\auth\models.py", line 178, in _user_get_all_permissions
    permissions.update(backend.get_all_permissions(user, obj))
  File "C:\projects\python3-venv\lib\site-packages\django\contrib\auth\backends.py", line 81, in get_all_permissions
    user_obj._perm_cache.update(self.get_group_permissions(user_obj))
  File "C:\projects\python3-venv\lib\site-packages\django\contrib\auth\backends.py", line 74, in get_group_permissions
    return self._get_permissions(user_obj, obj, 'group')
  File "C:\projects\python3-venv\lib\site-packages\django\contrib\auth\backends.py", line 57, in _get_permissions
    perms = getattr(self, '_get_%s_permissions' % from_name)(user_obj)
  File "C:\projects\python3-venv\lib\site-packages\django\contrib\auth\backends.py", line 41, in _get_group_permissions
    return Permission.objects.filter(**{user_groups_query: user_obj})
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\query.py", line 784, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\query.py", line 802, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\sql\query.py", line 1261, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\sql\query.py", line 1190, in build_filter
    self.check_related_objects(field, value, opts)
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\sql\query.py", line 1087, in check_related_objects
    self.check_query_object_type(value, opts, field)
  File "C:\projects\python3-venv\lib\site-packages\django\db\models\sql\query.py", line 1065, in check_query_object_type
    (value, opts.object_name))
ValueError: Cannot query "[email protected]": Must be "Group" instance.
>>> fmt.get_all_permissions()
set()

As you can see, the first the method is called a ValueError is thrown, but on the second time, it works as intended. This seems to be some kind of initialization problem, although I'm not doing any kind of explicit of initialization on the models. Is there any nuance when overriding this kind of relation? (used by other functions).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source