AttributeError: module 'django.db.models' has no attribute 'CASCAD'
时间: 2023-12-22 21:30:08 浏览: 50
这个错误是由于在引用中的代码中使用了错误的外键级联选项。正确的外键级联选项是`models.CASCADE`,而不是`CASCAD`。请修改代码中的错误拼写,将`CASCAD`改为`CASCADE`,然后重新运行代码即可。
```python
# mysite/polls/models.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
```
相关问题
AttributeError: module 'django.db.models' has no attribute
AttributeError: module 'django.db.models' has no attribute 'FieldDoesNotExist' 这个错误通常是由于导入的模块中没有该属性引起的。根据引用和引用的内容,可以看出解决这个问题的方法是将导入模块的代码进行修改。将原来的 from django.db.models import FieldDoesNotExist 改为 from django.core.exceptions import FieldDoesNotExist。这样就能正确导入 FieldDoesNotExist 属性了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [django安装xadmin及问题处理](https://blog.csdn.net/dangfulin/article/details/116785621)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
AttributeError: module 'django.db.models' has no attribute 'FortignKey'
AttributeError: module 'django.db.models' has no attribute 'FortignKey'一个常见的错误,它通常是由于拼写错误导致的。正确的拼写应该是 'ForeignKey' 而不是 'FortignKey'。ForeignKey 是 Django 框架中的一个关联字段,用于在模型之间建立关系。
如果你遇到了这个错误,可以检查以下几点:
1. 确保你正确导入了 django.db.models 模块:`from django.db import models`
2. 确保你正确拼写了 ForeignKey 字段:`models.ForeignKey(...)`
如果你已经检查了以上两点,仍然出现这个错误,可能是因为你使用了一个过时的版本的 Django,或者你的 Django 安装有问题。你可以尝试更新 Django 到最新版本,或者重新安装 Django。