Создание прибора из файла JSON в Django - PullRequest
0 голосов
/ 15 мая 2019

У меня проблемы с загрузкой моего прибора в 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:

1 Ответ

2 голосов
/ 15 мая 2019

Ваш прибор должен выглядеть так:

[
  {
    "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

...