zoukankan      html  css  js  c++  java
  • TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决办法

    TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决办法

    当执行 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=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)

  • 相关阅读:
    redo log 转csdn之ppp_10001
    Kafka的topic的partitions数的选取
    log4j:WARN No appenders could be found for logger
    HBase统计表的行数
    /bin/bash: /us/rbin/jdk1.8.0/bin/java: No such file or directory
    HBase shell命令
    Linux按名字杀死进程
    Kafka常用命令
    Plugin 'mavenassemblyplugin:' not found
    Linux搜索指定目录中所有文件的内容
  • 原文地址:https://www.cnblogs.com/shamoguzhou/p/15265727.html
Copyright © 2011-2022 走看看