我正在用 django/python 建立一个网站。我正在构建的网站数据库包含书籍,并且我试图通过在夹具中定义它们的关系来将 Character 对象与 Event 对象相关联。我使用 .json 文件加载我的装置
loaddata fixtures <file>
这适用于没有关系的模型,但是当我尝试加载包含多对多关系的固定装置时(字符出现在许多事件中,事件包含许多字符)我得到以下信息:
ValueError:问题安装夹具:<文件路径>:“<字符:Lanoree Brock >”需要在使用这种多对多关系之前为字段“字符”设置一个值。
我的 Character 模型中没有“character”字段:
class Character(models.Model):
id = models.IntegerField(primary_key = True)
name = models.CharField(max_length = ml)
bio = models.TextField()
event = models.ManyToManyField(Event)
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
我的 Character 夹具的 .json 文件如下所示:
[{"model": "library.Character", "id": 1,
"fields": {"name": "Lanoree Brock", "bio": "He lived", "event": [101, 102, ...]}}
... ]
所以错误发生在第一个字符。我对这个问题的猜测是 django 在保存对象之前尝试将关系添加到 < Character: Lanoree Brock > 对象,但我不明白它为什么这样做或如何解决它。
有没有办法构造夹具以确保在加载它时在代码尝试定义其关系之前创建/保存每个对象?
我是 django、JSON 语法和 web 开发的新手,我觉得我在这里没有做一些简单的事情——如果固定装置不能处理多对多关系,这似乎是它们功能上的一个巨大疏忽.
任何帮助将不胜感激,谢谢!
编辑:完整的错误日志:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\base.py", line 399, in execute
output = self.handle(*args, **options)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\commands\loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\commands\loaddata.py", line 100, in loaddata
self.load_label(fixture_label)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\managemen
t\commands\loaddata.py", line 158, in load_label
obj.save(using=self.using)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\core\serialize
rs\base.py", line 204, in save
setattr(self.object, accessor_name, object_list)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\db\models\fiel
ds\related_descriptors.py", line 480, in __set__
manager = self.__get__(instance)
File "c:\Users\MetalGearSamus\Anaconda\lib\site-packages\django\db\models\fiel
ds\related_descriptors.py", line 468, in __get__
return self.related_manager_cls(instance)
File "c:Users\MetalGearSamus\Anaconda\lib\site-packages\django\db\models\fiel
ds\related_descriptors.py", line 751, in __init__
(instance, self.source_field_name))
ValueError: Problem installing fixture 'c:Users\MetalGearSamus\Personal\Legends\website\library\fixtures\database.json': "<Character: Lanoree Brock>" needs to
have a value for field "character" before this many-to-many relationship can be
used.