MakeMigration Error on Django - ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'
MakeMigration Error on Django - ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'
添加新模型字段和 运行 makemigrations 命令后出现以下错误:
ImportError: 无法从 'django.db.models' 导入名称 'FieldDoesNotExist' (/usr/local/lib/python3.7/site-packages/django/db/models/init.py)
这是我的 models.py 的样子:
import uuid
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
# Create your models here.
class Book(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
price = models.DecimalField(max_digits=6, decimal_places=2)
cover = models.ImageField(upload_to='covers/', blank=True) # New Field
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('book_detail', args=[str(self.id)])
class Review(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='reviews')
review = models.CharField(max_length=255)
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return self.review
这是我的迁移文件的当前状态,我有两个。
0001_initial.py
# Generated by Django 3.0.8 on 2020-08-01 13:11
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('title', models.CharField(max_length=200)),
('author', models.CharField(max_length=200)),
('price', models.DecimalField(decimal_places=2, max_digits=6)),
],
),
]
0002_review.py
# Generated by Django 3.0.8 on 2020-08-06 11:21
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('books', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Review',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('review', models.CharField(max_length=255)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='books.Book')),
],
),
]
这是 运行 makemigration 命令后的异常回溯:
Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
web_1 | self.run()
web_1 | File "/usr/local/lib/python3.7/threading.py", line 870, in run
web_1 | self._target(*self._args, **self._kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
web_1 | raise _exception[1]
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
web_1 | autoreload.check_errors(django.setup)()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
web_1 | app_config.import_models()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
web_1 | self.models_module = import_module(models_module_name)
web_1 | File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package, level)
web_1 | File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/account/models.py", line 14, in <module>
web_1 | from .adapter import get_adapter
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/account/adapter.py", line 31, in <module>
web_1 | from ..utils import (
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/utils.py", line 15, in <module>
web_1 | from django.db.models import FieldDoesNotExist, FileField
web_1 | ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models' (/usr/local/lib/python3.7/site-packages/django/db/models/__init__.py)
所以,我不知道究竟是什么导致了这个错误,并停留在这个阶段。任何帮助将不胜感激。
您的 django-allauth
版本对 FieldDoesNotExist
的导入不正确。正确的导入是:
from django.core.exceptions import FieldDoesNotExist
来自 django.db.models
的导入大概适用于旧版本的 Django。
版本 0.41.0 中 django-allauth
中的导入 was fixed。如果您在 requirements.txt
或 pipenv 中更新 django-allauth
,它应该可以解决问题。
据我所知,您的版本为 3.0.8
。
但对我来说,当我将 requirements.txt
从 Django>=3.0.6
(使用最新的 3.1 Docker 图像)更改为 Django==3.0.6
时,它有所帮助,因为我使用的是最新的 3.1 版本,它似乎是第 3 方包的兼容性问题。
在 OP 的情况下是 django-allauth,在我的情况下是 django-filter。
检查错误日志中显示导入 from django.db.models import FieldDoesNotExist, FileField
文件的最后一行,然后尝试升级该包。
在 OP 的情况下,错误日志行是这样的:
File "/usr/local/lib/python3.7/site-packages/allauth/utils.py"...
在我的例子中,Django version 3.1.3
与 django-filter 2.0.0
冲突。
类似的错误信息。但是最后几行错误说是graphene-django
。然而,在中间,它说 django-filter
。当我查看 django-filter
源文件时,FieldDoesNotExist
导入错误。当我将 django-filter
升级到 version 2.4.0
时,问题解决了。
我的错误日志显示错误来自 rest_framework 应用程序
(文件“/home/Username_here/restapi-basics/lib/python3.8/site-packages/
rest_framework/serializers.py", line 25, in )。但我无法升级它(如上述解决方案中所述),因为它只是我添加到我的 Django 项目中的一个应用程序。
对我有用的是使用 pip 升级虚拟环境中的所有包。这是我使用的命令:
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
来源:How to upgrade all Python packages with pip
添加新模型字段和 运行 makemigrations 命令后出现以下错误:
ImportError: 无法从 'django.db.models' 导入名称 'FieldDoesNotExist' (/usr/local/lib/python3.7/site-packages/django/db/models/init.py)
这是我的 models.py 的样子:
import uuid
from django.contrib.auth import get_user_model
from django.db import models
from django.urls import reverse
# Create your models here.
class Book(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
price = models.DecimalField(max_digits=6, decimal_places=2)
cover = models.ImageField(upload_to='covers/', blank=True) # New Field
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('book_detail', args=[str(self.id)])
class Review(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='reviews')
review = models.CharField(max_length=255)
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return self.review
这是我的迁移文件的当前状态,我有两个。 0001_initial.py
# Generated by Django 3.0.8 on 2020-08-01 13:11
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('title', models.CharField(max_length=200)),
('author', models.CharField(max_length=200)),
('price', models.DecimalField(decimal_places=2, max_digits=6)),
],
),
]
0002_review.py
# Generated by Django 3.0.8 on 2020-08-06 11:21
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('books', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Review',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('review', models.CharField(max_length=255)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reviews', to='books.Book')),
],
),
]
这是 运行 makemigration 命令后的异常回溯:
Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/threading.py", line 926, in _bootstrap_inner
web_1 | self.run()
web_1 | File "/usr/local/lib/python3.7/threading.py", line 870, in run
web_1 | self._target(*self._args, **self._kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
web_1 | raise _exception[1]
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
web_1 | autoreload.check_errors(django.setup)()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
web_1 | app_config.import_models()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
web_1 | self.models_module = import_module(models_module_name)
web_1 | File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package, level)
web_1 | File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/account/models.py", line 14, in <module>
web_1 | from .adapter import get_adapter
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/account/adapter.py", line 31, in <module>
web_1 | from ..utils import (
web_1 | File "/usr/local/lib/python3.7/site-packages/allauth/utils.py", line 15, in <module>
web_1 | from django.db.models import FieldDoesNotExist, FileField
web_1 | ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models' (/usr/local/lib/python3.7/site-packages/django/db/models/__init__.py)
所以,我不知道究竟是什么导致了这个错误,并停留在这个阶段。任何帮助将不胜感激。
您的 django-allauth
版本对 FieldDoesNotExist
的导入不正确。正确的导入是:
from django.core.exceptions import FieldDoesNotExist
来自 django.db.models
的导入大概适用于旧版本的 Django。
版本 0.41.0 中 django-allauth
中的导入 was fixed。如果您在 requirements.txt
或 pipenv 中更新 django-allauth
,它应该可以解决问题。
据我所知,您的版本为 3.0.8
。
但对我来说,当我将 requirements.txt
从 Django>=3.0.6
(使用最新的 3.1 Docker 图像)更改为 Django==3.0.6
时,它有所帮助,因为我使用的是最新的 3.1 版本,它似乎是第 3 方包的兼容性问题。
在 OP 的情况下是 django-allauth,在我的情况下是 django-filter。
检查错误日志中显示导入 from django.db.models import FieldDoesNotExist, FileField
文件的最后一行,然后尝试升级该包。
在 OP 的情况下,错误日志行是这样的:
File "/usr/local/lib/python3.7/site-packages/allauth/utils.py"...
在我的例子中,Django version 3.1.3
与 django-filter 2.0.0
冲突。
类似的错误信息。但是最后几行错误说是graphene-django
。然而,在中间,它说 django-filter
。当我查看 django-filter
源文件时,FieldDoesNotExist
导入错误。当我将 django-filter
升级到 version 2.4.0
时,问题解决了。
我的错误日志显示错误来自 rest_framework 应用程序 (文件“/home/Username_here/restapi-basics/lib/python3.8/site-packages/ rest_framework/serializers.py", line 25, in )。但我无法升级它(如上述解决方案中所述),因为它只是我添加到我的 Django 项目中的一个应用程序。
对我有用的是使用 pip 升级虚拟环境中的所有包。这是我使用的命令:
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
来源:How to upgrade all Python packages with pip