我对Python和Django框架还很陌生,并且遇到了一个我找不到太多信息的问题,我目前正在关注有关django的教程: https://docs.djangoproject.com/en/2.2/intro/tutorial02/
我一直遇到此错误-
question = models.ForeignKey(Question,on_delete = models.cascade)
AttributeError:模块'django.db.models'没有属性'cascade'
我觉得有些简单的事情我没有做,并且希望得到一些帮助。
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)
它说我应该得到:
Migrations for 'polls':
polls/migrations/0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
答案 0 :(得分:2)
应为CASCADE
- (Ref: Django Doc)(在大写情况下为)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
因此,您的models.py
成为
#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)
答案 1 :(得分:0)
您应该使用CASCADE
而不是cascade
question = models.ForeignKey(Question, on_delete=models.CASCADE)