ImportError: cannot import name 'QUERY_TERMS'

2023-02-18 14:43:16 浏览数 (1)

错误背景(background )

Django 1.11 upgrade to Django 2.x

错误日志(error log)

代码语言:javascript复制
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:py_virtualenvjoyoolibsite-packagesdjango_filters__init__.py", line 4, in <module>
    from .filterset import FilterSet
  File "E:py_virtualenvjoyoolibsite-packagesdjango_filtersfilterset.py", line 16, in <module>
    from .filters import (Filter, CharFilter, BooleanFilter, BaseInFilter, BaseRangeFilter,
  File "E:py_virtualenvjoyoolibsite-packagesdjango_filtersfilters.py", line 11, in <module>
    from django.db.models.sql.constants import QUERY_TERMS
ImportError: cannot import name 'QUERY_TERMS'

解决办法(solution)

source code location:..Libsite-packagesdjango_filtersfilters.py

代码语言:javascript复制
try:
    # djagno 1.11
    from django.db.models.sql.constants import QUERY_TERMS
except ImportError:
    # Django 2.1
    QUERY_TERMS = {
        'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
        'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
        'month', 'day', 'week_day', 'hour', 'minute', 'second', 'isnull', 'search',
        'regex', 'iregex',
    }

0 人点赞