【问题标题】:Cleanest way to delete stale ContentTypes?删除陈旧内容类型的最干净方法?
【发布时间】:2017-01-08 10:14:52
【问题描述】:

我想摆脱运行迁移时收到的以下消息:

The following content types are stale and need to be deleted:

    appname | modelname

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?

在迁移中对删除或不删除的决定进行编码似乎更简洁。

作为迁移的一部分,检测和删除陈旧 ContentType 的最简单方法是什么?如果 ContentTypes 为created automatically,为什么不自动完成?

【问题讨论】:

  • 如果 generic relations 中使用了内容类型,删除它们将触发这些模型中外键的​​ on_delete 操作,并可能导致数据丢失——这是任何自动化操作都应该避免的费用。创建内容类型不会导致数据丢失,所以自动创建是安全的。
  • 无法检测内容类型是否用于泛型关系?

标签: django django-models django-migrations


【解决方案1】:

试试这个安全删除

python manage.py remove_stale_contenttypes

此命令可用> Django 1.11

【讨论】:

    【解决方案2】:

    迁移数据库时,您可以像这样将--noinput 添加到manage.py

    manage.py migrate --noinput
    

    这样,就不会询问用户。

    【讨论】:

    • 你确定这有效吗?迁移时从未有人问过我...
    • 我知道 :) 但我已经使用 django 大约 9 年了。而且我从未见过这会处理内容类型...
    • 很公平。 :-)
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 2015-06-19
    • 2020-11-02
    • 1970-01-01
    • 2015-01-19
    • 2021-08-01
    • 2012-11-01
    • 2017-08-17
    相关资源
    最近更新 更多