Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changed import structure in Django 3.2?

Tags:

python

django

I just opened a new Djangoproject with v3.2 and tried to import my apps as usual, when I got an ImproperlyConfigured Exception on one of my apps.

Steps:

  1. create and activate env, pip install django (without version number defaults to v3.2 as of now)
  2. django-admin startproject project
  3. mkdir project\apps, mkdir project\apps\core
  4. django-admin startapp core project\apps\core
  5. migrate, if necessary
  6. Go to settings and add 'apps.core' to INSTALLED_APPS

The result, when trying to runserver

\lib\site-packages\django\apps\config.py", line 246, in create
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'core'. Check that 
'apps.core.apps.CoreConfig.name' is correct.

Structure

-- project
  -- apps
    -- core
      -- apps.py
  -- project
  -- manage.py

core\apps.py

class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'

The auto_field seems to be new as it was not integrated in earlier versions, however, this should not affect the importation if I understand this correctly.

I returned to v3.1.3 to check if something else went wrong, but it is working fine with this approach.

Does anyone have the same issues or have a solution?

Thanks

like image 349
pan Avatar asked Sep 06 '25 03:09

pan


1 Answers

I hit the same problem, also on an app named apps.core (coincidence).

With Django >= 3.1, you're required to fully qualify the name:

class CoreConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.core'

Seems to work for 3.0 as well.

like image 115
Tim Nyborg Avatar answered Sep 08 '25 00:09

Tim Nyborg