python - 我可以从 django ForeignKey 中删除 null=True 参数吗?
问题描述
我有两个模型,foo
和bar
。它们已经存在于我的数据库中,每个都有很多实例。现在,我意识到应该添加一个关系。因此,我希望在foo
和之间添加一个 ForeignKey bar
。
class foo():
# lots of stuff
bar = models.ForeignKey(bar, related_name='foo', null=True)
我实际上不希望密钥可以为空,但由于它们已经存在,我需要添加它,因为需要填充现有行。
null=True
一旦所有实例都填充了 foreignKey 字段,我可以稍后删除该参数吗?
解决方案
Yes you can, basically to reach this you need to do three steps
- Create a first migration, where you create your field as nullable
- Create a second migration to populate already existing fields
- Create a third migration where you set the field as not nullable
A very good step by step is explained here
推荐阅读
- ios - 如何使用 Swift 获取推送通知状态?
- javascript - 为什么相同的 async/await 函数在某处工作而不在其他地方工作?
- c# - 使用 Razor Pages 在表格中显示数据时出现问题
- python - 如何在此查询中添加 if 条件?
- python - 得到 ImportError: cannot import name 'PandasError'
- javascript - 尝试离线导出到 Highcharts
- python-3.x - 如何从 AWS lambda 重建对 Amazon lex 的响应?
- python - 将列表传递给渲染(请求)时CSS失败
- android-fragments - “尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'”
- android - 单行显示 Insight RecyclerView