• <i id='BsThQ'><tr id='BsThQ'><dt id='BsThQ'><q id='BsThQ'><span id='BsThQ'><b id='BsThQ'><form id='BsThQ'><ins id='BsThQ'></ins><ul id='BsThQ'></ul><sub id='BsThQ'></sub></form><legend id='BsThQ'></legend><bdo id='BsThQ'><pre id='BsThQ'><center id='BsThQ'></center></pre></bdo></b><th id='BsThQ'></th></span></q></dt></tr></i><div id='BsThQ'><tfoot id='BsThQ'></tfoot><dl id='BsThQ'><fieldset id='BsThQ'></fieldset></dl></div>

    <small id='BsThQ'></small><noframes id='BsThQ'>

    <legend id='BsThQ'><style id='BsThQ'><dir id='BsThQ'><q id='BsThQ'></q></dir></style></legend>
    <tfoot id='BsThQ'></tfoot>
        • <bdo id='BsThQ'></bdo><ul id='BsThQ'></ul>

        在 Django 中对 CheckboxSelectMultiple 选项进行分组

        Grouping CheckboxSelectMultiple Options in Django(在 Django 中对 CheckboxSelectMultiple 选项进行分组)
          <tbody id='c1Wd3'></tbody>

        <small id='c1Wd3'></small><noframes id='c1Wd3'>

        <tfoot id='c1Wd3'></tfoot>
      1. <i id='c1Wd3'><tr id='c1Wd3'><dt id='c1Wd3'><q id='c1Wd3'><span id='c1Wd3'><b id='c1Wd3'><form id='c1Wd3'><ins id='c1Wd3'></ins><ul id='c1Wd3'></ul><sub id='c1Wd3'></sub></form><legend id='c1Wd3'></legend><bdo id='c1Wd3'><pre id='c1Wd3'><center id='c1Wd3'></center></pre></bdo></b><th id='c1Wd3'></th></span></q></dt></tr></i><div id='c1Wd3'><tfoot id='c1Wd3'></tfoot><dl id='c1Wd3'><fieldset id='c1Wd3'></fieldset></dl></div>

              <bdo id='c1Wd3'></bdo><ul id='c1Wd3'></ul>

              • <legend id='c1Wd3'><style id='c1Wd3'><dir id='c1Wd3'><q id='c1Wd3'></q></dir></style></legend>
                  本文介绍了在 Django 中对 CheckboxSelectMultiple 选项进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 Django 应用程序中,我有以下模型:

                  In my Django App I have the following model:

                  class SuperCategory(models.Model):
                    name = models.CharField(max_length=100,)
                    slug = models.SlugField(unique=True,)
                  
                  class Category(models.Model):
                    name            = models.CharField(max_length=100,)
                    slug            = models.SlugField(unique=True,)
                    super_category  = models.ForeignKey(SuperCategory)
                  

                  我在 Django 的管理界面中尝试完成的是使用小部件 CheckboxSelectMultiple 呈现 Category,但 Category 以某种方式按 SuperCategory 分组,像这样:

                  What I'm trying to accomplish in Django's Admin Interface is the rendering of Category using widget CheckboxSelectMultiple but with Category somehow grouped by SuperCategory, like this:

                  类别:

                  体育:<- 超类别项目
                  [ ] 足球 <- 类别项目
                  [ ] 棒球 <- 分类项目
                  [ ] ...

                  Sports: <- Item of SuperCategory
                  [ ] Soccer <- Item of Category
                  [ ] Baseball <- Item of Category
                  [ ] ...

                  政治:<- SuperCategory 的另一个项目
                  [ ] 拉丁美洲
                  [ ] 北美
                  [] ...

                  Politics: <- Another item of SuperCategory
                  [ ] Latin America
                  [ ] North america
                  [ ] ...

                  <小时>

                  有人对如何做到这一点有好的建议吗?


                  Does anybody have a nice suggestion on how to do this?

                  非常感谢.

                  推荐答案

                  经过一番挣扎,我得到了.

                  After some struggle, here is what I got.

                  首先,让ModelAdmin调用一个ModelForm:

                  First, make ModelAdmin call a ModelForm:

                  class OptionAdmin(admin.ModelAdmin):
                  
                     form = forms.OptionForm
                  

                  然后,在表单中,使用自定义小部件进行渲染:

                  Then, in the form, use use a custom widget to render:

                  category = forms.ModelMultipleChoiceField(queryset=models.Category.objects.all(),widget=AdminCategoryBySupercategory)    
                  

                  最后是小部件:

                  class AdminCategoryBySupercategory(forms.CheckboxSelectMultiple):
                  
                       def render(self, name, value, attrs=None, choices=()):
                           if value is None: value = []
                           has_id = attrs and 'id' in attrs
                           final_attrs = self.build_attrs(attrs, name=name)
                           output = [u'<ul>']
                           # Normalize to strings
                           str_values = set([force_unicode(v) for v in value])
                           supercategories = models.SuperCategory.objects.all()
                           for supercategory in supercategories:
                               output.append(u'<li>%s</li>'%(supercategory.name))
                               output.append(u'<ul>')
                               del self.choices
                               self.choices = []
                               categories = models.Category.objects.filter(super_category=supercategory)
                               for category in categories:
                                   self.choices.append((category.id,category.name))
                               for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
                                   if has_id:
                                       final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                                       label_for = u' for="%s"' % final_attrs['id']
                                   else:
                                       label_for = ''
                                   cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
                                   option_value = force_unicode(option_value)
                                   rendered_cb = cb.render(name, option_value)
                                   option_label = conditional_escape(force_unicode(option_label))
                                   output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
                               output.append(u'</ul>')
                               output.append(u'</li>')
                           output.append(u'</ul>')
                           return mark_safe(u'
                  '.join(output))
                  

                  不是最优雅的解决方案,但嘿,它奏效了.

                  Not the most elegant solution, but hey, it worked.

                  这篇关于在 Django 中对 CheckboxSelectMultiple 选项进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  • <small id='UaHgL'></small><noframes id='UaHgL'>

                    <tfoot id='UaHgL'></tfoot>

                    1. <i id='UaHgL'><tr id='UaHgL'><dt id='UaHgL'><q id='UaHgL'><span id='UaHgL'><b id='UaHgL'><form id='UaHgL'><ins id='UaHgL'></ins><ul id='UaHgL'></ul><sub id='UaHgL'></sub></form><legend id='UaHgL'></legend><bdo id='UaHgL'><pre id='UaHgL'><center id='UaHgL'></center></pre></bdo></b><th id='UaHgL'></th></span></q></dt></tr></i><div id='UaHgL'><tfoot id='UaHgL'></tfoot><dl id='UaHgL'><fieldset id='UaHgL'></fieldset></dl></div>
                        <tbody id='UaHgL'></tbody>

                    2. <legend id='UaHgL'><style id='UaHgL'><dir id='UaHgL'><q id='UaHgL'></q></dir></style></legend>

                            <bdo id='UaHgL'></bdo><ul id='UaHgL'></ul>