数据库 – Django:如作甚异构数据范例树建模?
发布时间:2021-01-26 05:35:59 所属栏目:编程 来源:网络整理
导读:我必要在我的数据库中存储树数据布局,我打算行使 django-treebeard或 django-mptt.我的夹杂源是每个节点也许是三种差异的也许范例之一:根节点将始终是范例A实体,叶节点是C类实体,其间的任何对象都是B类实体.我想知道模仿这种环境的最佳要领. 更新:我起首尝
我必要在我的数据库中存储树数据布局,我打算行使 django-treebeard或 django-mptt.我的夹杂源是每个节点也许是三种差异的也许范例之一:根节点将始终是范例A实体,叶节点是C类实体,其间的任何对象都是B类实体.我想知道模仿这种环境的最佳要领. 更新:我起首实行了模子担任,我以为这大噶?鲱好的要领.不幸的是,django-treebeard的民众API现实上并不是为处理赏罚这个而计划的.我最终获得它与GenericForeignKey一路事变.很是感激您的答复. 办理要领怎样行使模子中的 generic relation将树布局生涯到它所代表的节点的内容工具中?from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class Node(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() object = generic.GenericForeignKey('content_type','object_id') 在检索完备树的内容工具时,这也许会导致大量查询,可是有镌汰所需查询数目的ways and means. # Assuming mptt,as I'm not familiar with treebeard's API # 1 query to retrieve the tree tree = list(Node.tree.all()) # 4 queries to retrieve and cache all ContentType,A,B and C instances,respectively populate_content_object_caches(tree) (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |