Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

代码:

from django.db import models

# Create your models here.


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_time = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

错误:

解决办法:

将第十二行的代码改为:

question = models.ForeignKey(Question, on_delete=models.CASCADE,)

即在外键值的后面加上 on_delete=models.CASCADE


猜你喜欢

转载自blog.csdn.net/zoulonglong/article/details/79581247