khajvah khajvah - 2 months ago 11x
Python Question

Custom and Unmanaged User Model/Lagacy User Table

I am trying to make

work with my custom User model. The model is inherited from
and also from
. Here is the

class User(AbstractUser, GuardianUserMixin):

class Meta:
managed = False
db_table = 'users'

The problem I am facing is when I am checking permissions. For example here

Here is the error message:

ProgrammingError at /manageDb/mira/follow/14/
relation "users_groups" does not exist
LINE 1: ...sion"."group_id" = "auth_group"."id" ) INNER JOIN "users_gro...


File "/home/khajvah/Project/mira_website/manageDb/" in delete
486. if request.user.has_perm('follow', follow_object):
File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/" in has_perm
353. return _user_has_perm(self, perm, obj)
File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/" in _user_has_perm
281. if backend.has_perm(user, perm, obj):
File "/usr/local/lib/python3.4/dist-packages/guardian/" in has_perm
89. return check.has_perm(perm, obj)
File "/usr/local/lib/python3.4/dist-packages/guardian/" in has_perm
54. return perm in self.get_perms(obj)
File "/usr/local/lib/python3.4/dist-packages/guardian/" in get_perms
110. perms = list(set(chain(user_perms, group_perms)))
File "/usr/local/lib/python3.4/dist-packages/django/db/models/" in __iter__
162. self._fetch_all()
File "/usr/local/lib/python3.4/dist-packages/django/db/models/" in _fetch_all
965. self._result_cache = list(self.iterator())
File "/usr/local/lib/python3.4/dist-packages/django/db/models/" in iterator
1217. for row in compiler.results_iter():
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/" in results_iter
794. results = self.execute_sql(MULTI)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/sql/" in execute_sql
840. cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/" in execute
64. return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.4/dist-packages/django/db/" in __exit__
97. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.4/dist-packages/django/utils/" in reraise
658. raise value.with_traceback(tb)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/" in execute
64. return self.cursor.execute(sql, params)

The problem seems to be that
is making query on a table
, which doesn't exist, instead, it is called
, which is created by
. So the problem comes down to specifying the user group model in


I made a hacky solution. What was guardian doing is expecting a ManyToMany relationship with User and auth_group. AbstractUser does this by default but since I have manually given managed = False and also a db_table, Django wasn't creating the relationship when migrating, so I created a intermediate model for the relationship and here is how a part of my looks like:

class User(AbstractBaseUser, PermissionsMixin, GuardianUserMixin):

    username=models.CharField(unique=True, max_length=123)
    date_joined = models.DateTimeField(
    email=models.CharField(unique=True, max_length=125)

    is_staff = models.NullBooleanField(null=True)
    is_active = models.NullBooleanField(null=True)

    REQUIRED_FIELDS = ['password', 'email', 'first_name', 'last_name']

    objects = UserManager()
    class Meta:
        managed = False
        db_table = 'users'

class GroupUser(models.Model):
    group = models.ForeignKey(Group)
    user = models.ForeignKey(User)

    class Meta:
        db_table = 'users_groups'

The key thing is to have the group table to have the correct name. I.e. guardian is waiting for userTableName_groups.