我正在开发一个hierchical 应用程序,其中叶节点可以是不同模型的实例。我不知道如何使它与 django-mptt 应用程序一起工作。在那个应用程序中这甚至可能吗?如果是,我做错了什么?如果没有,有什么我想做的吗?
以下是模型的基本结构:
class FolderItemBase(MPTTModel):
order = models.PositiveIntegerField()
class Meta:
abstract = True
class MPTTMeta:
parent_attr = 'folder'
order_insertion_by = ['order']
class Folder(FolderItemBase):
folder = TreeForeignKey('Folder', related_name='folders', blank=True, null=True)
...
class Image(FolderItemBase):
folder = TreeForeignKey('Gallery', related_name='images') # cannot be null since leaf has to be inside of a folder
...
当我尝试执行以下操作时,我只能获得文件夹子项,而没有图像。当我尝试获取图像的祖先时也是如此
>>> folder1 = Folder.objects.create(title='Folder 1', order=0)
>>> folder2 = Folder(title='Folder 2', order=0)
>>> folder2.insert_at(folder1, save=True)
>>> image = Image(...)
>>> image.insert_at(folder1, save=True)
>>> folder1.get_children()
[<Folder: Folder 2>]
>>> image.get_ancestores()
[]
在这一切之后,这就是事物在数据库中的存储方式:
Folder table
----------------
+----+-------+-----+------+---------+-------+-----------+----------+
| ID | order | lft | rght | tree_id | level | folder_id | title |
+----+-------+-----+------+---------+-------+-----------+----------+
| 1 | 0 | 1 | 4 | 1 | 0 | | Folder 1 |
+----+-------+-----+------+---------+-------+-----------+----------+
| 2 | 0 | 2 | 3 | 1 | 1 | 1 | Folder 2 |
+----+-------+-----+------+---------+-------+-----------+----------+
Images Table
------------
+----+-------+-----+------+---------+-------+-----------+
| ID | order | lft | rght | tree_id | level | folder_id |
+----+-------+-----+------+---------+-------+-----------+
| 1 | 1 | 2 | 3 | 1 | 1 | 1 |
+----+-------+-----+------+---------+-------+-----------+
如您所见,它计算了图像的级别编号和正确的(至少看起来是)左右编号,但是它不会更新文件夹表中的任何内容,因此当您尝试进行查询时, 没有被选中。
任何指针表示赞赏。谢谢你。