将父母和孩子保存在数据库中

发布于 2025-02-03 04:34:11 字数 408 浏览 1 评论 0原文

我有三张桌子。

class Parent():
    pass

class Child():
    parent = models.ForiegnKey(Parent)

class GrandChild():
    child = models.ForiegnKey(Child)

现在父母可以有很多孩子,孩子有很多孙子,我的意思是100。

有什么方法可以同时保存父母/孩子/孙子?因此,我们只能获得 1 数据库命中,而父母/子/孙子都保存了。我知道Django中有可用的交易,但是根据我的理解(我可能是错误的)交易并不是解决此问题的解决方案。

I have Three tables.

class Parent():
    pass

class Child():
    parent = models.ForiegnKey(Parent)

class GrandChild():
    child = models.ForiegnKey(Child)

Now Parent can have many Childs and child have many GrandChilds, i mean 100's.

Is there any way to save the Parent/Child/Grandchild at the same time ? So we get only 1 database hit and Parent/Child/Grandchild are all saved. I know there is transaction available in Django, but as per my understanding (I may be wrong) transactions are not the solution to this problem.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文