【问题标题】:Creating fixture from json file in django在 django 中从 json 文件创建夹具
【发布时间】:2019-10-02 23:05:09
【问题描述】:

我在 Django 2.2 中加载我的夹具时遇到问题

我的固定文件夹中有一个名为 data.json 的文件,如下所示:

[{"model": "Headline", "pk": 1, "fields": {"article_link": "https://www.huffingtonpost.com/entry/versace-black-code_us_5861fbefe4b0de3a08f600d5", "headline": "former versace store clerk sues over secret 'black code' for minority shoppers", "is_sarcastic": 0}}, {"model": "Headline", "pk": 2, "fields": {"article_link": "https://www.huffingtonpost.com/entry/roseanne-revival-review_us_5ab3a497e4b054d118e04365", "headline": "the 'roseanne' revival catches up to our thorny political mood, for better and worse", "is_sarcastic": 0}}, {"model": "Headline", "pk": 3, "fields": {"article_link": "https://local.theonion.com/mom-starting-to-fear-son-s-web-series-closest-thing-she-1819576697", "headline": "mom starting to fear son's web series closest thing she will have to grandchild", "is_sarcastic": 1}}]

我有这个模型:

from django.db import models

class Headline(models.Model):
    article_link = models.CharField(max_length=250)
    headline = models.CharField(max_length=500)
    is_sarcastic = models.IntegerField()

我不断收到此错误,但不确定是什么问题?

django.core.serializers.base.DeserializationError:

【问题讨论】:

    标签: django django-fixtures


    【解决方案1】:

    您的灯具应如下所示:

    [
      {
        "model": "myapp.headline",
        "pk": 1,
        "fields": {
          "article_link": "https://foobar.com",
          "headline": "Test",
          "is_sarcastic": 1
        }
      },
      {
        "model": "myapp.headline",
        "pk": 2,
        "fields": {
          "article_link": "https://foobar.com",
          "headline": "Test",
          "is_sarcastic": 1
        }
      }
    ]
    

    你可以看这里:https://docs.djangoproject.com/en/2.2/howto/initial-data/#providing-data-with-fixtures

    【讨论】:

    • 对不起,我把 "model": "Headline" 改成了 "model": "sarcasm.headline"(这个应用叫做 sarcasm)——还是没有运气?
    • 你能在这里发布完整的追溯吗
    • 好的,感谢您的耐心等待我使用在线转换器将 json 转换为格式良好的 json,现在我得到无法加载 sarcasm.Headline(pk=1867): value too long for type character varying(250 ) - 我在 CharFields 上设置 max_length 的大小似乎并不重要
    • 您的 max_length 设置为不存在的 500。您可以使用带 max_length=255 的 CharField 或不带任何 max_length 的 TextField
    • 我确实更改了 TextField 并进行了迁移,但这并没有帮助
    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 2021-12-05
    • 2019-05-06
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多