question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'

See original GitHub issue

Tried to install the package with all requirements and following the steps described in README resulted in the following error when migrating:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "<env_dir>/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "<env_dir>/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "<env_dir>/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "<env_dir>/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "<env_dir>/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 24, in ready
    self.module.autodiscover()
  File "<env_dir>/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "<env_dir>/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 "/usr/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 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<env_dir>/lib/python3.8/site-packages/advanced_filters/admin.py", line 10, in <module>
    from .forms import AdvancedFilterForm
  File "<env_dir>/lib/python3.8/site-packages/advanced_filters/forms.py", line 13, in <module>
    from django.db.models import Q, FieldDoesNotExist
ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models' (<env_dir>/lib/python3.8/site-packages/django/db/models/__init__.py)

Steps: 1-) pip install django-advanced-filters Output:

Collecting django-advanced-filters
  Downloading django_advanced_filters-1.2.0-py3-none-any.whl (68 kB)
     |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 68 kB 704 kB/s 
Collecting django-braces<1.14.0,>=1.4.0
  Downloading django_braces-1.13.0-py2.py3-none-any.whl (15 kB)
Collecting simplejson<4,>=3.6.5
  Downloading simplejson-3.17.2-cp38-cp38-manylinux2010_x86_64.whl (137 kB)
     |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 137 kB 2.3 MB/s 
Installing collected packages: django-braces, simplejson, django-advanced-filters
Successfully installed django-advanced-filters-1.2.0 django-braces-1.13.0 simplejson-3.17.2

2-) added django-advanced-filters to projects settings.py; 3-) python manage.py makemigrations Output:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "<env_dir>/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "<env_dir>/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "<env_dir>/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "<env_dir>/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "<env_dir>/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/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 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<env_dir>/lib/python3.8/site-packages/advanced_filters/models.py", line 6, in <module>
    from .q_serializer import QSerializer
  File "<env_dir>/lib/python3.8/site-packages/advanced_filters/q_serializer.py", line 6, in <module>
    import six
ModuleNotFoundError: No module named 'six'

4-) pip install six Output:

Collecting six
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six
Successfully installed six-1.15.0

5-) added six to projects settings.py; 6-) python manage.py makemigrations and python manage.py migrate outputs the first described exception. 7-) added simplejson to projects settings.py; 😎 python manage.py makemigrations and python manage.py migrate also outputs the first described exception. 9-) removed six and simplejson from projects settings.py; 10-) python manage.py makemigrations and python manage.py migrate also outputs the first described exception.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
holgercocommented, Oct 30, 2020

this is occurred because we use django 3.1

3reactions
denizdogancommented, Jan 26, 2021

EDIT: My fix proposed below has already been implemented in the develop branch of this repo. D’oh!

@asfaltboy I think this could easily be fixed by using

from django.core.exceptions import FieldDoesNotExist

…instead of…

from django.db.models import Q, FieldDoesNotExist

…in forms.py.

It’s mentioned in the release notes for 3.1:

The compatibility import of django.core.exceptions.FieldDoesNotExist in django.db.models.fields is removed.

The move of the exception was made in 2015, which would correspond to Django 1.x, so I think it should be safe to use the new import! πŸ˜ƒ

Read more comments on GitHub >

github_iconTop Results From Across the Web

MakeMigration Error on Django - ImportError: cannot import ...
ImportError : cannot import name 'FieldDoesNotExist' from 'django.db.models' (/usr/local/lib/python3.7/site-packages/django/db/models/init.py).
Read more >
Cannot import name 'FieldDoesNotExist' from django.db.models
The "ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'" occurs because you have a package that tries to import the FieldDoesNotExistΒ ...
Read more >
MakeMigration Error on Django - ImportError: cannot import ...
Django : MakeMigration Error on Django - ImportError : cannot import name ' FieldDoesNotExist' from 'django.db.models ' [ Beautify YourΒ ...
Read more >
cannot import name 'fielddoesnotexist' from 'django ... - You.com
Describe the bug Django 3.1.5 crashes when trying to use django-advanced-filters. Errors generated. ImportError: cannot import name 'FieldDoesNotExist' from ' ...
Read more >
cannot import name 'fielddoesnotexist' from 'django.db.models ...
ImportError : cannot import name 'FieldDoesNotExist' from 'django.db.models.fields' (C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\Β ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found