在下面的示例中,您如何以及在哪里设置默认值?
author = models.ForeignKey('作者', on_delete=models.SET_DEFAULT)
最佳答案
对于 SET_DEFAULT
上类, default
应在 ForeignKey
中使用声明:
author = models.ForeignKey('Author', on_delete=models.SET_DEFAULT, default=None, null=True)
在这种情况下,无论设置为 default
当相关对象被删除时将被使用。
编辑:
正如 @MojixCoder 和 @Ersain 所指出的,在本例中您需要设置 null=True
否则删除ForeignKey的实例将导致 IntegrityError
待提高。
关于python - 如何设置Django的默认值 'on_delete=models.SET_DEFAULT',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68341967/