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(max_length=120)price = models.DecimalField(max_digits=9, decimal_places=2, default=0)class Project(models.Model):nid = models.AutoField(primary_key=True)name = models.CharField(max_length=120)price = models.DecimalField(max_digits=9, decimal_places=2, default=0)# 与Contract建立一对多的关系contract = models.ForeignKey(Contract)
当执行 python manage.py makemigrations 出现错误:TypeError: init() missing 1 required positional argument: ‘on_delete’
解决方案:
定义外键的时候需要加上 on_delete=;
即:contract = models.ForeignKey(Contract, on_delete=models.CASCADE)
原因如下:
django 升级到2.0之后,表与表之间关联的时候,必须要写on_delete参数,否则会报异常:
TypeError: init() missing 1 required positional argument: ‘on_delete’
on_delete各个参数的含义如下:
on_delete=None, # 删除关联表中的数据时,当前表与其关联的field的行为on_delete=models.CASCADE, # 删除关联数据,与之关联也删除on_delete=models.DO_NOTHING, # 删除关联数据,什么也不做on_delete=models.PROTECT, # 删除关联数据,引发错误ProtectedError# models.ForeignKey('关联表', on_delete=models.SET_NULL, blank=True, null=True)on_delete=models.SET_NULL, # 删除关联数据,与之关联的值设置为null(前提FK字段需要设置为可空,一对一同理)# models.ForeignKey('关联表', on_delete=models.SET_DEFAULT, default='默认值')on_delete=models.SET_DEFAULT, # 删除关联数据,与之关联的值设置为默认值(前提FK字段需要设置默认值,一对一同理)on_delete=models.SET, # 删除关联数据,a. 与之关联的值设置为指定值,设置:models.SET(值)b. 与之关联的值设置为可执行对象的返回值,设置:models.SET(可执行对象)
由于多对多(ManyToManyField)没有 on_delete 参数,所以以上只针对外键(ForeignKey)和一对一(OneToOneField)
以上是对学习过程中遇到问题的纪录,感谢以下文章的作者:
https://www.cnblogs.com/phyger/p/8035253.html
https://blog.csdn.net/buxianghejiu/article/details/79086011
欢迎指正,共同学习!:)
TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决办法相关推荐
- 【Python】django报错:TypeError: __init__() missing 1 required positional argument: 'on_delete'解决办法
错误代码: from __future__ import unicode_literals from django.db import models from django.utils.encodin ...
- 解决Django: [TypeError: __init__() missing 1 required positional argument: 'on_delete']
试用python3.7 + Django2.1.3的时候, 在创建模型类关联外键时, 报如下错误: Traceback (most recent call last):xxxxxxxxxxx Type ...
- Django:TypeError: __init__() missing 1 required positional argument: 'on_delete'
在Django框架中,有表与表间的关联时,使用命令:python manage.py makmigrations models中的创建表的代码: class Book(models.Model):na ...
- Django关联数据库时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'...
sgrade = models.ForeignKey("Grades",) 执行python manage.py makemigrations后出现 TypeError: __in ...
- django报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
个人博客页面链接:http://www.shihao.online (django搭建的个人博客,还在完善中) django报错 TypeError: __init__() missing 1 req ...
- Django - TypeError: __init__() missing 1 required positional argument: ‘on_delete‘ 的解决办法
Django 中创建Model时报以下错误: TypeError: init() missing 1 required positional argument: 'on_delete' 代码如下: f ...
- PyCharm 添加 外键的时候 报错 TypeError:__init__()missing 1 required positional argument:‘on_delete‘
主要是 Django 版本问题,由2.0 后就需要添加上这一块的信息. 在对应的对象 外键 ,例如: class Comment(models.Model):content = models.Text ...
- Django 生成数据库表时的报错TypeError: __init__() missing 1 required positional argument: 'on_delete'...
原因及解决办法: https://www.cnblogs.com/phyger/p/8035253.html 转载于:https://www.cnblogs.com/yun1108/p/9197195 ...
- Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
code: 1 #encoding=utf-82 from django.db import models3 # Create your models here.4 class BookInfo(mo ...
最新文章
- python制作酷炫动画_厉害了!Python+matplotlib制作8个排序算法的动画
- ckplayer---vue
- 四个角度教你评估一个产品的用户体验好坏 | PMCAFF
- boost::hana::less用法的测试程序
- 不自由な放課後 + 正しいメイドの躾かた 汉化补丁
- listitem android,android-为contextmenu标识listitem的ID
- string类的相关知识及部分操作
- Linux的实际操作:用户管理(用户组的管理groupadd useradd usermod)
- 为程序员而骄傲的飞鸽传书
- C/C++信息隐写术(二)之字符串藏入BMP文件
- 自然语言处理 —— 2.3 词嵌入的特性
- 给ubuntu换内核
- 深入了解C++的广泛应用
- Mysql中的delimiter详解
- 魔鬼作坊端游脱机封包辅助制作
- PyTorch学习(九)--用CNN模型识别手写数字数据集MNIST
- 快讯 | 科技部公布首批国家人工智能创新平台名单,BAT+科大讯飞入选
- 我的世界服务器末地文件在哪,我的世界:找不到末地城怎么办?巧用2个操作,就能瞬间找到!...
- 初体验 | 购买激活Typora软件
- 【C语言】汉诺塔问题(图文详解)