我正在使用django / python构建一个网站。我正在构建的网站数据库包含书籍,我试图通过在夹具中定义它们之间的关系来将Character对象与Event对象相关联。我使用
从.json文件加载我的灯具loaddata fixtures <file>
这适用于没有关系的模型,但是当我尝试加载包含多对多关系的夹具(字符出现在许多事件中,事件包含许多字符)时,我得到以下内容:
ValueError:安装夹具时遇到问题:&lt; 文件路径&gt;:&#34;&lt; 角色:Lanoree Brock &gt; &#34;需要有字段值#34;字符&#34;在此之前可以使用多对多关系。
没有字段&#34;字符&#34;在我的角色模型中:
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 fixture的.json文件如下所示:
[{"model": "library.Character", "id": 1,
"fields": {"name": "Lanoree Brock", "bio": "He lived", "event": [101, 102, ...]}}
... ]
因此错误发生在第一个角色。我对这个问题的猜测是django试图将关系添加到&lt; 角色:Lanoree Brock &gt;对象在保存对象之前,但我不明白它为什么这样做或如何解决它。
有没有办法构建夹具以确保在加载时每个对象都是在代码尝试定义其关系之前创建/保存的?
我是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.