gpt4 book ai didi

python - Django反序列化错误安装Fixture问题

转载 作者:行者123 更新时间:2023-12-03 19:45:23 26 4
gpt4 key购买 nike

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 。有没有其他方法,如果是,请帮助我?

最佳答案

Fixtures 文件必须匹配 django serialization format ,例如:

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

所以你需要用以下方式重写你的灯具:
  • 添加model
  • 添加 pk 字段
  • 将其余字段移动到 fields 内部对象
  • 关于python - Django反序列化错误安装Fixture问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617353/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com