Django报错:__init__() missing 1 required positional argument: 'on_delete'
原因:
在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
举例说明:
user=models.OneToOneField(User)
owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值
参数说明:
on_delete有CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET()五个可选择的值
CASCADE:此值设置,是级联删除。
PROTECT:此值设置,是会报完整性错误。
SET_NULL:此值设置,会把外键设置为null,前提是允许为null。
SET_DEFAULT:此值设置,会把设置为外键的默认值。
SET():此值设置,会调用外面的值,可以是一个函数。
一般情况下使用CASCADE就可以了。
Django报错:__init__() missing 1 required positional argument: 'on_delete'的更多相关文章
- Django问题 TypeError: __init__() missing 1 required positional argument: 'on_delete'
问题:在执行python manage.py makemigrations learning_logs时,系统会报错,提示:TypeError: __init__() missing 1 requir ...
- Django在根据models生成数据库表时报错: __init__() missing 1 required positional argument: 'on_delete'
原因: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing ...
- Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...
- Django关联数据库时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
sgrade = models.ForeignKey("Grades",) 执行python manage.py makemigrations后出现TypeError: __ini ...
- Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
from django.db import models # Create your models here. class Category(models.Model): caption = mode ...
- Python3:Django根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
Python3:Django根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete' 一.分析 在 ...
- 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...
- Django2.1在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
解决办法: a=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) 即在外键值的后面加上 on_delete=models.CASCADE ...
- python进行数据库迁移的时候显示(TypeError: __init__() missing 1 required positional argument: 'on_delete')
进行数据库迁移的时候,显示 TypeError: __init__() missing 1 required positional argument: 'on_delete' 图示: 出现原因: 在 ...
随机推荐
- php输出textarea数据(入库没有处理的)
str_replace("\r\n","<br />",$xmactivity['xmdetail']) 导出excel换行方法 str_repla ...
- OpenCV批量读入(处理)
#include <windows.h> #include <iostream> #include <opencv2/opencv.hpp> using names ...
- Mybatis批量更新和插入
<update id="updateOrInsert"> <foreach collection="list" index="ind ...
- EF CodeFirst学习笔记001--主键约定
Code First 的核心是约定,这些默认的规则使我们可以用我们自己的类来创建模型.EF框架要求一个类必须有一个键属性.规则约定如果一个属性名为Id或者是类名+Id的形式(如PatientId),这 ...
- 一、Iterator 迭代器
需求:如何不适用for循环,依次遍历出数组中每个元素? 设计原理: 代码清单: 接口类 public interface Iterator { boolean hasNext(); Object ne ...
- 解决ie浏览器下载apk或ipa变为zip
Tomcat/conf/web.xml <mime-mapping> <extension>apk</extension> <mime-type>app ...
- Android笔记:ContextMenu
ContextMenu,称为上下文菜单,也就是长按界面不放,弹出的菜单.使用ContextMenu有三个步骤: (1)调用registerForContextMenu()方法,为视图注册上下文菜单: ...
- 设置input标签的placeholder的样式
设置input样式代码: input::-webkit-input-placeholder{ /*WebKit browsers*/ color: red; } input::-moz-input-p ...
- vim常用命令之多行注释和多行删除
vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率. 1.多行注释: 1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式; ...
- shell中参数的传递
1.命令行参数 向shell脚本传递数据的最基本方式是使用命令行参数. (1) 读取参数 读取输入的参数的变量为位置参数,位置参数通过标准数字表示, 其中$0为程序名称,$1为第一个参数,$2为第二个 ...