Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS #155

Open
khaliullin opened this issue Nov 30, 2022 · 5 comments
Labels

Comments

@khaliullin
Copy link

Does this library support Django 3.2.6?

I did the following steps according to documentation:

  • In setting.py INSTALLED_APPS block I replaced django.contrib.sessions to user_sessions
  • In setting.py MIDDLEWARE replaced django.contrib.sessions.middleware.SessionMiddleware to user_sessions.middleware.SessionMiddleware
  • In setting.py added
    SESSION_ENGINE = 'user_sessions.backends.db'
    SILENCED_SYSTEM_CHECKS = ['admin.E410']
  • In projects main urls.py added path('', include('user_sessions.urls', namespace='user_sessions'))
    When I am making python manage.py migrate I get the following error:
    RuntimeError: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Difference in versions

Latest version in release notes is 2.0.0
But in pypi.org the latest version is 1.7.1.

I have installed 1.7.1 using pip. How can I get the 2.0.0 version? Maybe this would fix the error I get.

  • Python version: 3.8.10
  • Django version: 3.2.6
  • django-otp version:
  • django-user-sessions version: 1.7.1
@WhyNotHugo
Copy link
Member

Looks like you're trying to use django.contrib.sessions.models.Session, but this model is provided by the contrib-app that is replaced by this one.

Can you post the full traceback?

@khaliullin
Copy link
Author

@WhyNotHugo
I removed default django session app and middleware.

Here is my full log

Starting the server

/user/app/.venv/bin/python /user/app/manage.py runserver 8000 
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/user/app/.venv/lib/python3.8/site-packages/sentry_sdk/integrations/threading.py", line 69, in run
    reraise(*_capture_exception())
  File "/user/app/.venv/lib/python3.8/site-packages/sentry_sdk/_compat.py", line 54, in reraise
    raise value
  File "/user/app/.venv/lib/python3.8/site-packages/sentry_sdk/integrations/threading.py", line 67, in run
    return old_run_func(self, *a, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/user/app/.venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/user/app/.venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/user/app/.venv/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/user/app/.venv/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/user/app/.venv/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/user/app/.venv/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user/app/landing/admin.py", line 13, in <module>
    from settings.admin import SettingsAdmin
  File "/user/app/settings/admin.py", line 39, in <module>
    class GlobalSettingsAdmin(SettingsAdmin):
  File "/user/app/.venv/lib/python3.8/site-packages/django/contrib/admin/decorators.py", line 100, in _model_admin_wrapper
    admin_site.register(models, admin_class=admin_class)
  File "/user/app/.venv/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 141, in register
    self._registry[model] = admin_class(model, self)
  File "/user/app/settings/admin.py", line 27, in __init__
    model.load()
  File "/user/app/core/models.py", line 3682, in load
    return cls.objects.get_or_create()[0]
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 581, in get_or_create
    return self.get(**kwargs), False
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 431, in get
    num = len(clone)
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 47, in __iter__
    db = queryset.db
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 1253, in db
    return self._db or router.db_for_write(self.model, **self._hints)
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/utils.py", line 239, in _route_db
    for router in self.routers:
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/utils.py", line 230, in routers
    router = import_string(r)()
  File "/user/app/.venv/lib/python3.8/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/user/app/main/dbrouters/primary_router.py", line 6, in <module>
    from django.contrib.sessions.models import Session
  File "/user/app/.venv/lib/python3.8/site-packages/django/contrib/sessions/models.py", line 10, in <module>
    class Session(AbstractBaseSession):
  File "/user/app/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 113, in __new__
    raise RuntimeError(
RuntimeError: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

@WhyNotHugo
Copy link
Member

WhyNotHugo commented Dec 26, 2022 via email

@blag
Copy link
Contributor

blag commented Oct 9, 2023

@khaliullin Any objections to closing this?

@WhyNotHugo
Copy link
Member

My above message is garbled up, but to sumarise:

The error message ends with:

RuntimeError: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

This says django.contrib.sessions.models.Session. That's not the model provided by this library, it's the one provided by django.contrib.sessions. You're importing the wrong model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants