我正在尝试使用以下 yaml 代码初始化一些 Django 模型:
- model: app.block &block_1
pk: 1
- model: app.vertex
fields:
x: 804837.500000
y: 7508312.500000
z: 326.000000
block: *block_1
- model: app.vertex
fields:
x: 804837.500000
y: 7508312.500000
z: 334.000000
block: *block_1
- model: app.vertex
fields:
x: 804837.500000
y: 7508362.500000
z: 326.000000
block: *block_1
但是,当调用“python manage.py loaddata ./data.yaml”来填充数据库时,我收到一条错误消息说
alizers/pyyaml.py", line 62, in Deserializer
raise DeserializationError(e)
DeserializationError: found undefined alias 'block_1'
in "./data/initial.yaml", line 9, column 12
由于某种原因,无法识别锚块_1。有没有人如何正确放置锚?yaml格式还有什么问题吗?
干杯,马克斯
更新:我尝试从 Django 导出数据以了解语法。我得到了这个结果:
$ python manage.py dumpdata app --format=yaml
- fields: {block: 1, x: 1.0, y: 2.0, z: 3.0}
model: app.vertex
pk: 1
- fields: {block: 2, x: 4.0, y: 5.0, z: 6.0}
model: app.vertex
pk: 2
- fields: {}
model: app.block
pk: 1
- fields: {}
model: app.block
pk: 2
- fields: {}
model: app.block
pk: 3
这很没用。导出似乎没有捕获实际上存在的引用(通过管理界面检查)。
我还使用了在线解析器。当它通过解析器时,Django 引发了一个异常:
- block: &block_1
model: app.block
pk: 1
- vertex:
model: app.vertex
fields:
x: 804837.500000
y: 7508312.500000
z: 326.000000
block: *block_1
引起
File "/Library/Python/2.7/site-packages/Django-1.4.1-py2.7.egg/django/core/management/commands/loaddata.py", line 190, in handle
for obj in objects:
File "/Library/Python/2.7/site-packages/Django-1.4.1-py2.7.egg/django/core/serializers/pyyaml.py", line 62, in Deserializer
raise DeserializationError(e)
DeserializationError: 'model'