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

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'

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

时间: 2024-10-10 13:16:28

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

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

Django 提供完善的模型(model)层主要用来创建和存取数据,不需要我们直接对数据库操作.Django 模型基础知识: 1.每个模型是一个 Python 类,继承 django.db.models.model 类. 2.该模型的每个属性表示一个数据库表字段. 程序代码如下: # 创建应用程序数据表模型(对应数据库的相关操作) from django.db import models # 导入models模块 class Event(models.Model): # 创建Event类,继承m

执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决方法: 在连接外键时加上: on_delete=models.CASCADE 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument

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

1.产生原因:在创建外键后,执行 python manage.py makemigrations报错,具体代码为: from django.db import models class Classes(models.Model): title = models.CharField(max_length=32) m = models.ManyToManyField("Teachers") class Teachers(models.Model): name = models.CharFi

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

解决办法: a=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) 即在外键值的后面加上 on_delete=models.CASCADE 原因: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing 1 required positional argument: 'on_delete'举例说明:us

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

code: 1 #encoding=utf-8 2 from django.db import models 3 # Create your models here. 4 class BookInfo(models.Model): #创建书本信息类,继承models.Model 5 booktitle=models.CharField(max_length=20) 6 bookdata=models.DateField() 7 class HeroInfo(models.Model): #创建英

Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: ‘on_delete’

Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: 'on_delete' 代码如下: from django.db import models Create your models here. class Contract(models.Model): nid = models.AutoField(primary_key=True) name = models.CharField(ma

Django2.0在models创建数据表时报错

Django2.0在models创建数据表时报错TypeError: __init__() missing 1 required positional argument: 'on_delete' 如下创建了两个表,Tag 表以 Contact 表为外部键,此时可以看到pycharm控制台报错 123456789101112131415 class (models.Model): name = models.CharField(max_length=200) age = models.Intege

Python3+Django2.0基础入门demo

 1.安装Python3环境 [root@openshift ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 默认为Python2.7,需要安装Python3 [root@openshift ~]#wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh [root@openshift ~]# yum install bzip2 [

PowerDesigner生成数据库表和逆向生成表结构(MySQL数据库)

一.Download Connector/ODBC下载ODBC驱动,地址:https://dev.mysql.com/downloads/connector/odbc/, 需要注意:PowerDesigner安装的多少位就下载多少位的,一般是32位,建议下载.msi文件直接安装. 二.安装完成后点击powerdesigner的Database--->Configure Connections...--->添加数据源配置,如下图: 三.PDM模型生成数据库sql文件,点击powerdesigne