您的位置:首页 > 博客中心 > 数据库 >

【python3】 django2.0 在生成数据库表时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete'

时间:2022-03-15 06:45

python: 3.6.4

django: 2.0

models.py 代码如下

# coding: utf-8
from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Category(models.Model):
    name = models.CharField(max_length=100)
class Tag(models.Model):
    name = models.CharField(max_length=100)
class Post(models.Model):
    title = models.CharField(max_length=70)
    body = models.TextField()
    created_time = models.DateTimeField()
    modified_time = models.DateTimeField()
    excerpt = models.CharField(max_length=200, blank=True)
    category = models.ForeignKey(Category,on_delete=models.CASCADE)
    tags = models.ManyToManyField(Tag, blank=True)
    author = models.ForeignKey(User,on_delete=models.CASCADE)

 

错误信息: 

技术分享图片

 

 解决办法: 

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

category = models.ForeignKey(Category)
author = models.ForeignKey(User)

改为

category = models.ForeignKey(Category,on_delete=models.CASCADE)
author = models.ForeignKey(User,on_delete=models.CASCADE)

 

 再次执行 ,成功

技术分享图片

 



 

热门排行

今日推荐

热门手游