Django反序列化错误安装夹具时出现问题

d6kp6zgx  于 2022-12-14  发布在  Go
关注(0)|答案(4)|浏览(114)
Traceback (most recent call last):
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializer
    yield from PythonDeserializer(objects, **options)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/python.py", line 91, in Deserializer
    Model = _get_model(d["model"])
KeyError: 'model'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
    self.load_label(fixture_label)
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
    for obj in objects:
  File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/json.py", line 73, in Deserializer
    raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/Users/sparshkedia/Desktop/task/movie_rs/movies.json'

当我试图将json文件反序列化到数据库中时,显示了上面的错误。
我的json文件看起来像这样:

[
  {
    "description": "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy in a boy's room.",
    "genre": "Animation,Adventure,Comedy,Family,Fantasy",
    "imdb_url": "https://www.imdb.com/title/tt0114709/",
    "img_url": "https://m.media-amazon.com/images/M/MV5BMDU2ZWJlMjktMTRhMy00ZTA5LWEzNDgtYmNmZTEwZTViZWJkXkEyXkFqcGdeQXVyNDQ2OTk4MzI@._V1_UX182_CR0,0,182,268_AL__QL50.jpg",
    "movie_id": 114709,
    "title": "Toy Story",
    "users_rating": 8.3,
    "year": 1995
  },
  {
    "description": "George Banks must deal not only with the pregnancy of his daughter, but also with the unexpected pregnancy of his wife.",
    "genre": "Comedy,Family,Romance",
    "imdb_url": "https://www.imdb.com/title/tt0113041/",
    "img_url": "https://m.media-amazon.com/images/M/MV5BOTEyNzg5NjYtNDU4OS00MWYxLWJhMTItYWU4NTkyNDBmM2Y0XkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_UX182_CR0,0,182,268_AL__QL50.jpg",
    "movie_id": 113041,
    "title": "Father of the Bride Part II",
    "users_rating": 6,
    "year": 1995
  }]

我需要做什么来把json文件输入到数据库中?我还创建了相应的电影模型,其中包含了每个json文件的所有字段。
我正在使用python manage.py loaddata movies.json来完成这个任务。是否有其他方法,如果有,请帮助我?

w8f9ii69

w8f9ii691#

Fixtures文件必须符合django序列化格式,例如:

[
    {
        "pk": "4b678b301dfd8a4e0dad910de3ae245b",
        "model": "sessions.session",
        "fields": {
            "expire_date": "2013-01-16T08:16:59.844Z",
            ...
        }
    }
]

因此,您需要以下列方式重写您的fixture:
1.添加model密钥
1.添加pk字段
1.将其余字段移动到fields内部对象

yhxst69z

yhxst69z2#

我在manage.py中有打印语句。该输出被保存在fixture json文件中,该文件产生格式错误。删除这些打印语句解决了它。以下是我的旧(格式错误)fixture.json文件。我删除了第一行,它工作了。

app.yaml file = dev_app.yaml
[
  {
    "model": "plans.planslevel1",
    "pk": 1,
    "fields": {
      "name": "abc",
      "description": "xyz"
    }
  
  }]
goqiplq2

goqiplq23#

有时对象未正确关闭

  • 打开JSON文件,查看pycharmIDE突出显示的错误。
  • 如果有任何对象未关闭,请手动修复。
djmepvbi

djmepvbi4#

在django 4.1中你可以使用(doc):

python manage.py loaddata ./fixtures/YOUR_DATA.json

和固定装置结构必须是这样的,例如:

[
    {
        "model": "movie.movie",
        "pk": 1,
        "fields": {
            "description": "A cowboy doll is profoundly threatened and jealous when a new spaceman figure supplants him as top toy in a boy's room.",
            "genre": "Animation,Adventure,Comedy,Family,Fantasy",
            "imdb_url": "https://www.imdb.com/title/tt0114709/",
            "img_url": "https://m.media-amazon.com/images/M/MV5BMDU2ZWJlMjktMTRhMy00ZTA5LWEzNDgtYmNmZTEwZTViZWJkXkEyXkFqcGdeQXVyNDQ2OTk4MzI@._V1_UX182_CR0,0,182,268_AL__QL50.jpg",
            "movie_id": 114709,
            "title": "Toy Story",
            "users_rating": 8.3,
            "year": 1995
        }
    }
]

相关问题