Django:简单层次结构的最佳途径?(Django: Best way for simple hierarchy?)

我有这个模型:

class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent = models.ForeignKey('self', blank=True, null=True)

我希望Django根据层次结构对类别进行排序,例如:

家长1 孩子1 家长2 孩子1

我做了一些研究,发现了2个应用程序,treebeard.al_tree和Django MPTT,两者都很强大,可能导致性能下降或难以维护。

我将在网站的侧边栏和内部管理页面中显示类别(包括文章模型中的ForeignKey),对类别的添加/修改/删除将会非常少,大多数情况下只读,这对应用程序性能没有太大影响。

有没有其他的应用程序提供这个和比以上更简单? 我可以使用Django来实现这个功能,无需额外的应用程序,使用经理或其他东西?

I have this model:

class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent = models.ForeignKey('self', blank=True, null=True)

I want Django to sort the categories with respect to their hierarchy, for example:

Parent 1 Child 1 Parent 2 Child 1

I did some research and found 2 apps, treebeard.al_tree and Django MPTT, both are powerful which may lead to lower performance or harder to maintain.

I will display the categories in website's sidebar and inside admin pages (includes ForeignKey in posts model), there will be very low additions/modifications/deletions to categories, mostly reading only which should have no big affect on performance.

Is there any other app which offers this and simpler than those above? Can I achieve this using Django without additional apps, using managers or something else?

最满意答案

我没有看到使用像django-mptt这样的应用程序的任何不利之处。 实际上,它提供的方法经过优化,可以在使用分层结构进行查询时为您提供最大的性能。 没有理由担心可维护性和/或性能,而且使用起来非常简单!

I do not see any disadvantes using an app like django-mptt. In fact the methods provided by it are optimized to give you a maximum in performance when doing queries with hierarchical structures. No reason for me to worry about maintainability and/or performance and it is quite easy to use!

Django:简单层次结构的最佳途径?(Django: Best way for simple hierarchy?)

我有这个模型:

class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent = models.ForeignKey('self', blank=True, null=True)

我希望Django根据层次结构对类别进行排序,例如:

家长1 孩子1 家长2 孩子1

我做了一些研究,发现了2个应用程序,treebeard.al_tree和Django MPTT,两者都很强大,可能导致性能下降或难以维护。

我将在网站的侧边栏和内部管理页面中显示类别(包括文章模型中的ForeignKey),对类别的添加/修改/删除将会非常少,大多数情况下只读,这对应用程序性能没有太大影响。

有没有其他的应用程序提供这个和比以上更简单? 我可以使用Django来实现这个功能,无需额外的应用程序,使用经理或其他东西?

I have this model:

class Category(models.Model): name = models.CharField() description = models.CharField(blank=True) parent = models.ForeignKey('self', blank=True, null=True)

I want Django to sort the categories with respect to their hierarchy, for example:

Parent 1 Child 1 Parent 2 Child 1

I did some research and found 2 apps, treebeard.al_tree and Django MPTT, both are powerful which may lead to lower performance or harder to maintain.

I will display the categories in website's sidebar and inside admin pages (includes ForeignKey in posts model), there will be very low additions/modifications/deletions to categories, mostly reading only which should have no big affect on performance.

Is there any other app which offers this and simpler than those above? Can I achieve this using Django without additional apps, using managers or something else?

最满意答案

我没有看到使用像django-mptt这样的应用程序的任何不利之处。 实际上,它提供的方法经过优化,可以在使用分层结构进行查询时为您提供最大的性能。 没有理由担心可维护性和/或性能,而且使用起来非常简单!

I do not see any disadvantes using an app like django-mptt. In fact the methods provided by it are optimized to give you a maximum in performance when doing queries with hierarchical structures. No reason for me to worry about maintainability and/or performance and it is quite easy to use!