Py学习  »  Django

AttributeError:模块'django.db.models'没有属性'cascade'

Basil • 5 年前 • 1710 次点击  

我对Python和Django框架还比较陌生,遇到了一个我找不到太多信息的问题,我目前正在学习Django教程: https://docs.djangoproject.com/en/2.2/intro/tutorial02/

question=models.ForeignKey(问题,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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53789
 
1710 次点击  
文章 [ 2 ]  |  最新文章 5 年前
shafik
Reply   •   1 楼
shafik    6 年前

你应该用 CASCADE cascade

question = models.ForeignKey(Question, on_delete=models.CASCADE)
JPG
Reply   •   2 楼
JPG    6 年前

应该是 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)