首页 > 解决方案 > django smart 在 Django 版本 3.0.1 上选择 - 错误 ImportError: cannot import name 'six' from 'django.utils'

问题描述

已安装 django-smart-selects (pip install django-smart-selects) 并且不适用于 django 版本 3.0.1

我使用官方安装指南进行了配置。

enter code here $ python manage.py runserver
    Watching for file changes with StatReloader
    Exception in thread django-main-thread:
    Traceback (most recent call last):
      File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
        self.run()
      File "/usr/lib/python3.7/threading.py", line 865, in run
        self._target(*self._args, **self._kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
        autoreload.raise_last_exception()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
        raise _exception[1]
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
        autoreload.check_errors(django.setup)()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
        fn(*args, **kwargs)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
        app_config.import_models()
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
        self.models_module = import_module(models_module_name)
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
      File "<frozen importlib._bootstrap>", line 983, in _find_and_load
      File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 728, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/home/mxcloud3/Desktop/django/polls/models.py", line 2, in <module>
        from smart_selects.db_fields import GroupedForeignKey
      File "/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/smart_selects/db_fields.py", line 6, in <module>
        from django.utils import six
    ImportError: cannot import name 'six' from 'django.utils' (/home/mxcloud3/Desktop/django/venv/lib/python3.7/site-packages/django/utils/__init__.py)

安装片段

模型.py

from django.db import models
from smart_selects.db_fields import GroupedForeignKey

class Recipe(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    subcategory = GroupedForeignKey(Subcategory, "category", on_delete=models.CASCADE)

设置.py

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'smart_selects',
]

JQUERY_URL = 真

标签: djangodjango-modelsdjango-formsdjango-templatesdjango-views

解决方案


smart_selectsfrom django.utils import six在其代码的某处执行,这会导致导入错误,因为该包已在 django 3.0 中删除。

如果您无法更新有问题的软件包(在这种情况下您不能),唯一的解决方案是自己修补它,或者等到库的所有者修补它。

自己修补它是微不足道的:

  • pip3 install six
  • 导航到您的虚拟环境的 Django 安装。使用这里的 virtualenv:/path/to/python/ site-packages/django/utils/__init__.py
  • 添加import six

或者更好的是,使用 bash 单行代码:

pip3 install six && echo import six >"$(python3 -c "import sys; print(tuple(filter(lambda x: 'site-packages' in x, sys.path))[0])")"/django/utils/__init__.py

引号中的python3 -c脚本在很大程度上取决于能否确定site_packages目录的位置,并且在某些虚拟环境中不起作用。YMMV


推荐阅读