【发布时间】:2020-09-22 08:40:58
【问题描述】:
我正在尝试使用 manage.py loaddata 从 json 文件中导入数据。导入时出现以下错误。
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/django/core/serializers/python.py", line 144, in Deserializer
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/runner/BookShop/bookShopAPI/inventory/fixtures/seed_data.json': 'ManyToOneRel' object has no attribute 'to_python': (inventory.author:pk=2) field_value was '[1]'
models.py
class Genre(models.Model):
# TAG Choices
FICTION = 'F'
GRAPHIC_NOVEL = 'GN'
# add more tag options if required
TAG = (
(FICTION, 'Fiction'),
(GRAPHIC_NOVEL, 'Graphic Novel'),
)
# write implementation
tag = models.CharField(max_length=15,choices=TAG)
description = models.TextField(max_length=100)
author = models.ForeignKey(Author,on_delete=models.CASCADE,related_name='genre',null=True, blank=True)
book = models.ForeignKey('Book',on_delete=models.CASCADE,related_name='genre',null=True, blank=True)
class Author(models.Model):
# write implementation
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
author_bio = models.TextField(max_length=100)
book = models.ForeignKey('Book',on_delete=models.CASCADE,related_name='author',null=True, blank=True)
json 文件
[
{
"model":"inventory.genre",
"pk":"1",
"fields":{
"tag":"fiction",
"description":"Fiction is the telling of stories which are not real. More specifically, fiction is an imaginative form of narrative, one of the four basic rhetorical modes. Although the word fiction is derived from the Latin fingo, fingere, finxi, fictum, works of fiction need not be entirely imaginary and may include real people, places, and events. Fiction may be either written or oral. Although not all fiction is necessarily artistic, fiction is largely perceived as a form of art or entertainment. The ability to create fiction and other artistic works is considered to be a fundamental aspect of human culture, one of the defining characteristics of humanity."
}
},
{
"model":"inventory.genre",
"pk":"2",
"fields":{
"tag":"graphic novel",
"description":"A graphic novel is a narrative work in which the story is conveyed to the reader using sequential art in either an experimental design or in a traditional comics format. The term is employed in a broad manner, encompassing non-fiction works and thematically linked short stories as well as fictional stories across a number of genres."
}
},
{
"model": "inventory.author",
"pk": "1",
"fields": {
"first_name": "Arthur",
"last_name": "Golden",
"author_bio": "Arthur Golden was born in Chattanooga, Tennessee, and was educated at Harvard College, where he received a degree in art history, specializing in Japanese art. In 1980 he earned an M.A. in Japanese history from Columbia University, where he also learned Mandarin Chinese. Following a summer in Beijing University, he worked in Tokyo, and, after returning to the United States, earned an M.A. in English from Boston University. He resides in Brookline, Massachusetts, with his wife and two children.",
"genre":[
1
]
}
}]
模型有问题吗? 从归档流派的 json 数据导入作者 1 时似乎遇到了问题。在作者处引用类型 1 的正确方法应该是什么?
【问题讨论】:
-
你不能通过 Django 管理员添加模型吗?如果使用 import-export 包有很多信息?
-
我可以使用管理员和序列化器视图集添加数据,但我很好奇这个错误是关于什么的。
-
你的问题解决了吗:3?一样