Django:存储分层数据

发布于 2024-09-12 03:53:22 字数 693 浏览 9 评论 0原文

我正在尝试将文档的各个部分存储在 Django 应用程序中。该模型如下所示:

class Section(models.Model):
  project = models.ForeignKey(Project)
  parent_section = models.ForeignKey('Section', blank=True, null=True, related_name='child_set')
  predecessor_section = models.ForeignKey('Section', blank=True, null=True, related_name='predecessor_set')
  name = models.CharField(max_length=100)
  text = models.TextField(blank=True, null=True)

我创建了很多部分,链接它们(parent_section、predecessor_section)并通过调用它们的每个保存方法来存储它们。但是,当我保存后查看表时,parent_section_id 和predecessor_section_id 未设置,即使我在保存之前附加了对象。

我认为这与以下事实有关:某些parent_section实例没有分配id,因为它们的实例尚未存储,但使用手动事务无法解决问题。

对此有什么想法吗?

干杯, 最大限度

I'm trying to store sections of a document in a Django app. The model looks like:

class Section(models.Model):
  project = models.ForeignKey(Project)
  parent_section = models.ForeignKey('Section', blank=True, null=True, related_name='child_set')
  predecessor_section = models.ForeignKey('Section', blank=True, null=True, related_name='predecessor_set')
  name = models.CharField(max_length=100)
  text = models.TextField(blank=True, null=True)

I create a whole lot of sections, link them (parent_section, predecessor_section) and store them by calling each of their save methods. However, when I look into the table after saving it, the parent_section_id and the predecessor_section_id are not set, even though I had objects attached to them before saving.

I assume it has to do with the fact that some parent_section instances don't have an id assigned as their instance hasn't been stored yet, but using manual transactions couldn't solve the problem.

Any thoughts on that?

Cheers,
Max

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-09-19 03:53:22

在将对象保存到 Django ORM 中之前,它们没有 id。

所以我想说你需要 save() 对象,然后在父/子部分中引用它(并重新保存这些部分)。

然而,将 prec 和 next 作为指针存储的另一种选择是存储 sequence_index (间隔 10 以允许进一步插入而无需重新排序)并按此索引排序。

objects do not have an id until you save them in Django ORM.

So I'd say you need to save() the object, then reference it in your parent/child sections (and re-save the sections).

However, another option to storing prec and next as pointers is to store an sequence_index (spaced by 10 to allow further inserts wiothout reordering) and order by this index.

擦肩而过的背影 2024-09-19 03:53:22

尝试对所有对象执行 save() 操作,然后更新它们的关系,然后再次 save() 所有对象。

当您分配外键时,相关(目标)对象的 ID 会被复制。因为在分配关系(parent_section、predecessor_section)时,相关对象还没有 id,所以你会得到一个奇怪的结果:

A = Section(name='A')
B = Section(name='B')
B.parent_section = A
A.save() 
B.save()
B.parent_section # this will say A
B.parent_section_id # this will say **None**

但这应该有效:

A = Section(name='A')
B = Section(name='B')
A.save() 
B.save()
B.parent_section = A
B.parent_section # this will say A
B.parent_section_id # this will say A.id
B.save() # don't forget this one :)

Try doing a save() on all the objects, then update their relations, and then save() all of them again.

When you assign a foreignkey, the related (target) object's id is copied. since at the moment of assigning the relations (parent_section, predecessor_section) the related objects don't have an id yet, you get a funky result:

A = Section(name='A')
B = Section(name='B')
B.parent_section = A
A.save() 
B.save()
B.parent_section # this will say A
B.parent_section_id # this will say **None**

But this should work:

A = Section(name='A')
B = Section(name='B')
A.save() 
B.save()
B.parent_section = A
B.parent_section # this will say A
B.parent_section_id # this will say A.id
B.save() # don't forget this one :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文