【问题标题】:Django datadump and loaddata not working due to fixture error由于夹具错误,Django datadump 和 loaddata 无法正常工作
【发布时间】:2020-02-25 23:27:36
【问题描述】:

这是我尝试转储 mysql DB 的方法:

 python3 manage.py dumpdata > dumpdata.json

然后,我尝试重新加载它:

python3 manage.py loaddata dumpdata.json

这是我得到的错误:

json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 226398200 (char 226398199)

django.core.serializers.base.DeserializationError: Problem installing fixture '/home/dumpdata.json': 

【问题讨论】:

    标签: django loaddata


    【解决方案1】:

    通常的情况是您在应用程序引导过程中打印某些内容。 您打印的那些内容最终会出现在您的 fixture.json 中,从而弄乱您的 JSON 结构。

    为了使内容更具可读性,您可以使用:

    python3 manage.py dumpdata --indent 4 --natural-primary --natural-foreign -e contenttypes -e auth.Permission -e sessions > dumpdata.json

    一些细节:

    • --indent 4 将“美化”您的 JSON 输出,检查您的数据会更容易
    • -e sessions 将删除 session 的应用数据。 contenttype 也一样,它经常把事情搞砸,adminauth.Permission
    • --natural-primary --natural-foreign 将使用一些更自然的 PK 而不是 ID(例如,当您有一些 unique=True 字段时)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多