modlinltd/django-advanced-filters

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

renatdd opened this issue · 9 comments

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;
8-) 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.

Hello!
Exception ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models, I decided by installing the last commit from this repository
pip install -U https://github.com/modlinltd/django-advanced-filters/archive/3fc6b964c48c38fff6169f55f2c26325aa926dfb.zip.

There was another exception ImportError: cannot import name 'six' from 'django.utils'
Which is fixed in the pull request to the django-braces repository
And this can also be fixed by installing a commit
pip install -U https://github.com/galuszkak/django-braces/archive/5c6604123eaa73f53875c5dce5843a6598b7b52c.zip

this is occurred because we use django 3.1

Thanks for looking into this @holgerco! @pythaqua and @daveusa31 can you confirm that you are using Django 3.1?

While we don't yet support 3.1 (or 3.2-dev), PRs are always super welcome 🙇

Thanks for looking into this @holgerco! @pythaqua and @daveusa31 can you confirm that you are using Django 3.1?

While we don't yet support 3.1 (or 3.2-dev), PRs are always super welcome 🙇

Yes, I used 3.1

Thanks for looking into this @holgerco! @pythaqua and @daveusa31 can you confirm that you are using Django 3.1?

While we don't yet support 3.1 (or 3.2-dev), PRs are always super welcome bow

Hi there!
Confirming Django 3.1.2 here.
Thanks for the advices.

Thanks for looking into this @holgerco! @pythaqua and @daveusa31 can you confirm that you are using Django 3.1?

While we don't yet support 3.1 (or 3.2-dev), PRs are always super welcome bow

Perhaps the README.md requirements should be updated in this regard, right?

The readme says:

Django >= 1.9 (Django 1.9 - 3.0 on Python 2/3/PyPy3)

I think it can be made more clear. Feel free to submit a PR with improvements

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! :)