码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-02-26 11:35:34      阅读:906      评论:0      收藏:0      [点我收藏+]

标签:div   png   position   blog   osi   数据   tom   image   数据库表   

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)

 

 再次执行 ,成功

技术分享图片

 



 

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

标签:div   png   position   blog   osi   数据   tom   image   数据库表   

原文地址:https://www.cnblogs.com/richerdyoung/p/8471744.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!