python - 如何为 Django 的“on_delete=models.SET_DEFAULT”设置默认值
问题描述
在下面的示例中,您如何以及在何处设置默认值?
author = models.ForeignKey('Author', on_delete=models.SET_DEFAULT)
解决方案
For SET_DEFAULT
to work,default
应该在ForeignKey
声明中使用:
author = models.ForeignKey('Author', on_delete=models.SET_DEFAULT, default=None, null=True)
default
在这种情况下,删除相关对象时将使用设置为的任何内容。
编辑:正如@MojixCoder 和@Ersain 所指出的,在此示例中,您需要设置null=True
否则删除 ForeignKey 的实例将导致IntegrityError
引发。
推荐阅读
- python - 匹配的查询不存在。Django 错误
- sql - SQL 在几个日期之前获取 count(*)
- tableau-api - 在 LOD 中使用 RANK:详细级别表达式不能包含表计算或 Tableau 中的 attr 函数
- php - Yajra Datatables 编辑打印布局
- python - 拟合多项式回归时形状未对齐
- c# - 如何使用 LIST 进行过滤
在 LINQ 中 - networking - What can be done to avoid hosts file entry?
- node.js - Import Express Routes causes Request Body to be Undefined
- windows - no sound difference with multiband compression plugin juce
- perl - 从 perl 脚本运行时,无法获取 wget 命令以将响应结果输出到文件