FieldDoesNotExist 与 ManytoMany 字段
FieldDoesNotExist with ManytoMany field
我在 运行 迁移时遇到问题,我收到此错误:
django.core.exceptions.FieldDoesNotExist: Profile has no field named 'following
但我已经在我的模型中定义了字段,可能是什么问题?
下面是我的模型:
class Profile(TimeStampedModel, models.Model):
last_name = models.CharField(_('Last Name'), max_length=150,
null=True, blank=True)
gender = models.CharField(_('Gender'), null=True, max_length=30,
choices=GENDER_CHOICES, blank=True)
following = models.ManyToManyField(User, related_name='following',
default=None, blank=True)
可能是什么问题?
编辑
下面是堆栈跟踪:
https://gist.github.com/huxaiphaer/abd376f43d50625bd3b10fa4829d6b0e
迁移文件:
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0013_auto_20211108_1641'),
]
operations = [
migrations.AddField(
model_name='profile',
name='following',
field=models.ManyToManyField(blank=True, default=None,
related_name='following',
to=settings.AUTH_USER_MODEL),
)
]
文件:0013_auto_20211108_1641
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0012_auto_20211108_0608'),
]
operations = [
migrations.AlterField(
model_name='profile',
name='following',
field=models.ManyToManyField(blank=True, default=None, null=True, related_name='following', to=settings.AUTH_USER_MODEL),
),
]
当您已经有一个 following
字段时,您可能进行了迁移 0013,因此这导致了一个 AlterField
迁移,对于(尚未)的多对多关系由迁移构建。
您可以通过删除 AlterField
操作来修复 0013 迁移文件,从而使其成为“无操作迁移”:
从 django.conf 导入设置
来自 django.db 导入迁移、模型
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0012_auto_20211108_0608'),
]
<strong>operations = []</strong>
这将再次导致迁移 运行。
不过,我建议 不要 将来删除迁移。通常它只会导致迁移回到正轨的问题。
我在 运行 迁移时遇到问题,我收到此错误:
django.core.exceptions.FieldDoesNotExist: Profile has no field named 'following
但我已经在我的模型中定义了字段,可能是什么问题?
下面是我的模型:
class Profile(TimeStampedModel, models.Model):
last_name = models.CharField(_('Last Name'), max_length=150,
null=True, blank=True)
gender = models.CharField(_('Gender'), null=True, max_length=30,
choices=GENDER_CHOICES, blank=True)
following = models.ManyToManyField(User, related_name='following',
default=None, blank=True)
可能是什么问题?
编辑
下面是堆栈跟踪:
https://gist.github.com/huxaiphaer/abd376f43d50625bd3b10fa4829d6b0e
迁移文件:
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0013_auto_20211108_1641'),
]
operations = [
migrations.AddField(
model_name='profile',
name='following',
field=models.ManyToManyField(blank=True, default=None,
related_name='following',
to=settings.AUTH_USER_MODEL),
)
]
文件:0013_auto_20211108_1641
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0012_auto_20211108_0608'),
]
operations = [
migrations.AlterField(
model_name='profile',
name='following',
field=models.ManyToManyField(blank=True, default=None, null=True, related_name='following', to=settings.AUTH_USER_MODEL),
),
]
当您已经有一个 following
字段时,您可能进行了迁移 0013,因此这导致了一个 AlterField
迁移,对于(尚未)的多对多关系由迁移构建。
您可以通过删除 AlterField
操作来修复 0013 迁移文件,从而使其成为“无操作迁移”:
从 django.conf 导入设置 来自 django.db 导入迁移、模型
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('profiles', '0012_auto_20211108_0608'),
]
<strong>operations = []</strong>
这将再次导致迁移 运行。
不过,我建议 不要 将来删除迁移。通常它只会导致迁移回到正轨的问题。