RamezIssac/django-erp-framework

django.contrib.admin.sites.NotRegistered: The model CustomUser is not registered

Closed this issue · 4 comments

sina95 commented

When I use custom auth, in settings.py AUTH_USER_MODEL = 'core.CustomUser', they throw me this error.

Hello , Can you give me a full stack trace and more info ?

sina95 commented

When include django-erp-framework inside django project where use custom class for User, in settings.py file I add AUTH_USER_MODEL = 'core.CustomUser'

and in models.py my class inherit from AbstractUser and also i have registered model in my admin.py file.
When I comment AUTH_USER_MODEL, code works with default User class.

class CustomUser(AbstractUser):
id = models.AutoField(primary_key=True)
allowedLoginAttempts = models.PositiveSmallIntegerField(default=5)
profitCenter = models.ManyToManyField(ProfitCenter)
firstLineAgent = models.BooleanField(default=False)
refferalCode = models.PositiveSmallIntegerField(default=1)
organizations = models.ManyToManyField(Organization, through=Organization.user.through)

Exception is below:

Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run
autoreload.raise_last_exception()
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/core/management/init.py", line 381, in execute
autoreload.check_errors(django.setup)()
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
app_config.ready()
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 27, in ready
self.module.autodiscover()
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/contrib/admin/init.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/utils/module_loading.py", line 57, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 848, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/mscom/programming/dsd/accounting/core/admin.py", line 22, in
from erp_framework.admin.admin import erp_admin_site, EntityAdmin, TransactionAdmin, TransactionItemAdmin
File "/home/x/programming/education/academy/venv/src/django-erp-framework/erp_framework/admin/admin.py", line 41, in
from tabular_permissions.admin import (
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/tabular_permissions/admin.py", line 52, in
admin.site.unregister(User)
File "/home/x/programming/education/academy/venv/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 157, in unregister
raise NotRegistered('The model %s is not registered' % model.name)
django.contrib.admin.sites.NotRegistered: The model CustomUser is not registered

That's an issue with Django tabular permission place in the installed apps or its auto implementation.
Check its docs (or remove it)
I will add this to the faq.

sina95 commented

I fixed issues with confirming tabular permissions entry on widget with CustomUser auth model. I override tabular_permissions.js so form entry match customuser form:

    $('#customuser_form').on('submit', function () {
        var user_perms = [];
        var table_permissions = $('#tabular_permissions');
        var input_name = table_permissions.attr('data-input-name');
        table_permissions.find("input[type=checkbox]").not('.select-all').each(function (i, elem) {
            var $elem = $(elem);
            if ($(elem).prop('checked')) {
                user_perms.push($elem.attr('data-perm-id'))
            }
        });
        var user_group_permissions = $('[name=' + input_name +']');
        var output = [];
        $.each(user_perms, function (key, value) {
            output.push('<option value="' + value + '" selected="selected" style="display:none"></option>');
        });
        user_group_permissions.append(output);
    })