Django树建模 [英] Django tree modeling

查看:105
本文介绍了Django树建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个完全失去的新手,试图弄清楚如何使链接动态生成链接的硬编码列表,不仅仅是在学习Django时的noob练习,还可以通过管理员编辑数据。重要的是,它通过db生成时看起来完全一样,因为它现在处于硬编码状态。



主要这里的内容需要由模型来表示:



http: /www.drugpolicyreformmovement.com/directory



我想我先做一个类别标题的类别表,然后创建一个链接表与ForeignKey关系的链接回到类别。我认为在模板中,我会使用ifchanged模板标签与(nested?)for循环一起循环遍历每个标题,显示每个标题的子记录。



这个变得复杂的是在研究与&儿童纪录有自己的孩子纪录的教育标题。潜在的,尤其是当我重用这个代码为其他项目,我需要适应任意数量的小孩级别。



我花了两天时间查看文档和答案在这里和我在模型,视图或模板中,仍然没有足够清楚的图片。



例如,你如何横渡下来孩子级别在模板中查看元素是否有子元素? / />

解决方案

你所做的是:使用< a href =https://github.com/django-mptt/django-mptt =noreferrer> django-mptt 。



几乎是在数据库中存储节点树的规范解决方案,并以最小的数据库访问方式高效地检索。它包含一些有用的模板标签,可以帮助您输出想要的东西。


I am a completely lost newbie trying to figure out how to make my hard-coded list of links dynamically generated, not only as a noob exercise in learning Django but also so the data can be edited via admin. The important thing is that it looks the exact same when generated via the db as it does now in its hard-coded state.

The main content here needs to be represented by models:

http://www.drugpolicyreformmovement.com/directory

I think that I would first make a 'Category' table of category headings, then make a 'link' table of links with a ForeignKey relationship back to Category. I think that in the template I would use the 'ifchanged' template tag in concert with (nested?) for loops to iterate through each heading in turn, displaying each heading's child records.

Where this gets complicated is under the 'Research & Education' heading where a child record has its own child record. Potentially, esp. when I reuse this code for other projects, I would need to accommodate an arbitrary number of child levels.

I have spent two days looking through the docs and answers here and I still don't have a clear enough picture of what this would look like in models, views, or templates.

For instance, how do you traverse 'down' the child levels in the template to see if an element has children? How do you do all this w/o hitting the db a thousand times per view?

解决方案

What you do is: use django-mptt.

This is pretty much the canonical solution to storing a tree of nodes in the database and retrieving it efficiently with minimal db access. It includes some useful template tags that should help you output things how you want them.

这篇关于Django树建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆