Django:AttributeError:“NoneType”对象没有属性“is_relation”

Posted

技术标签:

【中文标题】Django:AttributeError:“NoneType”对象没有属性“is_relation”【英文标题】:Django: AttributeError: 'NoneType' object has no attribute 'is_relation' 【发布时间】:2020-01-09 02:49:26 【问题描述】:

Django的新模型类无法成功迁移,报错,这个错误需要怎么改

Django 2.1.7
Python 3.6.5

在之前版本的ORM中,也可以正常迁移,但是改了一些代码后就不能正常迁移了。

文件 "C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\core\management\com mands\migrate.py",第 203 行,在句柄中 fake_initial=fake_initial,文件“C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\db\migrations\execu tor.py”,第 117 行,在迁移中 state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fak e_initial) 文件 "C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\db\migrations\execu tor.py”,第 147 行,在 _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 文件 "C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\db\migrations\execu tor.py”,第 244 行,在 apply_migration 中 state = migration.apply(state, schema_editor) 文件 "C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\db\migrations\migra tion.py”,第 114 行,在应用中 operation.state_forwards(self.app_label, project_state) 文件“C:\Users\senyu\Anaconda3\envs\py3.6.5\lib\site-packages\django\db\migrations\opera tions\fields.py",第 144 行,在 state_forwards 中 delay = not old_field.is_relation AttributeError: 'NoneType' 对象没有属性 'is_relation'

【问题讨论】:

如果您没有显示任何代码或您正在做什么以产生错误,您希望我们如何帮助您?请用minimal reproducible example写你的问题。 【参考方案1】:

您遇到了 Django 错误:https://code.djangoproject.com/ticket/28073。您的一次迁移中可能有一个migrations.RemoveField。不知何故,您的数据库表和迁移不同步了。

似乎解决方案是检查并手动编辑您的迁移。我无法提供确切的解决方案,因为这取决于您的型号。

【讨论】:

以上是关于Django:AttributeError:“NoneType”对象没有属性“is_relation”的主要内容,如果未能解决你的问题,请参考以下文章

Python 报错 AttributeError: module 'django.db.models' has no attribute 'SubfieldBase'(

AttributeError: (Class) object has no attribute '__name__' 创建 ModelForms [Django & Python2.7]

Django 提示:AttributeError : ‘module‘ object has no attribute ‘utcnow‘

Django,gunicorn,nginx 抛出 504 Gateway Time-out: AttributeError: module 'static' has no attribute 'Cli

django-AttributeError: 'str' object has no attribute 'decode'

django-AttributeError: 'str' object has no attribute 'decode'