Django:在管理多重选择中显示嵌套类别

发布于 2024-10-02 16:05:34 字数 593 浏览 1 评论 0原文

我想找到一种获取类别和子类别的方法 以多选的形式显示在管理中。

例如:

parent 
----child1 
----child2 
parent2 
----child3 

我必须创建自定义字段还是已经有解决方案 大约?


编辑

模型为:

class Category(models.Model):

    def __unicode__(self):
        return self.name_en

    name = models.CharField(_('name'), max_length=255, null=True)
    slug = models.SlugField(_('slug'), db_index=True, unique=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child')   
    description =  models.TextField(_('description'), null=True)

I would like to find a way to get categories and subcategories
displayed in the admin, in the form of a multiple select.

Like:

parent 
----child1 
----child2 
parent2 
----child3 

Do I have to make a custom field or is there already a solution
around?


Edit

the model is:

class Category(models.Model):

    def __unicode__(self):
        return self.name_en

    name = models.CharField(_('name'), max_length=255, null=True)
    slug = models.SlugField(_('slug'), db_index=True, unique=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child')   
    description =  models.TextField(_('description'), null=True)

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

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

发布评论

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

评论(1

暗藏城府 2024-10-09 16:05:34

您不需要自定义字段,只需要自定义小部件。这是我制作的一个示例小部件。它未经测试,所以将其视为伪代码:)

from django.forms.widgets import SelectMultiple
from django.db.models import *

class Category(Model):
    name = TextField()
    parent = ForeignKey('self', null=True, related_name='children'):

class CategoryTreeWidget(SelectMultiple):
    def render_options(self, choices, selected_choices):
        selected_choices=set([force_unicode(v) for v in selected_choices])
        top_level_cats = Category.objects.filter(parent=None)
        def _render_category_list(cat_list, level=0):
            for category in cat_list:
                self.render_option(selected_choices, category.pk, (("---"*level + " ") if level) + category.name)
                def _render_category_list(category.children, level+1)
        _render_category_list(top_level_cats)

class Article(Model):
    title = TextField()
    body = TextField()
    category = ManyToMany('Category', widget = CategoryTreeWidget)

You don't need a custom field, just a custom widget. Here is an example widget i cooked up. it's untested, so treat it like pseudo-code :)

from django.forms.widgets import SelectMultiple
from django.db.models import *

class Category(Model):
    name = TextField()
    parent = ForeignKey('self', null=True, related_name='children'):

class CategoryTreeWidget(SelectMultiple):
    def render_options(self, choices, selected_choices):
        selected_choices=set([force_unicode(v) for v in selected_choices])
        top_level_cats = Category.objects.filter(parent=None)
        def _render_category_list(cat_list, level=0):
            for category in cat_list:
                self.render_option(selected_choices, category.pk, (("---"*level + " ") if level) + category.name)
                def _render_category_list(category.children, level+1)
        _render_category_list(top_level_cats)

class Article(Model):
    title = TextField()
    body = TextField()
    category = ManyToMany('Category', widget = CategoryTreeWidget)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文