作为数据导入的一部分,我有一些代码可以创建自定义页面对象:
instance = PerformancePage(
run=run,
date=json_data['date'],
time=json_data['time'],
price=json_data['price'],
title=f'{run.title} {json_data["date"]} {json_data["id"]}',
content_type=ContentType.objects.get_for_model(PerformancePage)
)
perf = run.add_child(instance=instance)
这有时会引发:
django.core.exceptions.ValidationError: {'path': ['Page with this Path already exists.']}
一些调试代码确实表明存在另一个具有相同路径的页面:
except ValidationError:
print('error attempting to save', instance)
print('path', instance.path)
print('is leaf', run.is_leaf())
rivals = Page.objects.filter(path=instance.path)
print(rivals.last().specific.run == run)
为什么会这样?
尝试手动增加竞争路径以设置新路径也不起作用:
instance.path = rivals.last().specific._inc_path()
perf = run.add_child(instance=instance)
# still raises
有趣的是,如果我跳过这些异常并继续导入,当我打印出这些路径时,它们似乎遵循类似的模式:
path 00010001000T0005000D0001
path 00010001000T000800050001
path 00010001000T000900060001
path 00010001000T000A00050001
path 00010001000T000A00050001
path 00010001000T000A00070001
path 00010001000T000A00070001
path 00010001000T000A00030001
这可能是相关的吗?