Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:53:35+00:00 2026-06-16T03:53:35+00:00

How can I group checkboxes produced by CheckboxSelectMultiple by a related model? This is

  • 0

How can I group checkboxes produced by CheckboxSelectMultiple by a related model?

This is best demonstrated by example.

models.py:

class FeatureCategory(models.Model):
    name = models.CharField(max_length=30)

class Feature(models.Model):
    name = models.CharField(max_length=30)
    category = models.ForeignKey(FeatureCategory)

class Widget(models.Model):
    name = models.CharField(max_length=30)
    features = models.ManyToManyField(Feature, blank=True)

forms.py:

class WidgetForm(forms.ModelForm):
    features = forms.ModelMultipleChoiceField(
        queryset=Feature.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False
    )
    class Meta:
        model = Widget

views.py:

def edit_widget(request):
    form = WidgetForm()
    return render(request, 'template.html', {'form': form})

template.html:

{{ form.as_p }}

The above produces the following output:

[] Widget 1
[] Widget 2
[] Widget 3
[] Widget 1
[] Widget 2

What I would like is for the feature checkboxes to be grouped by feature category (based on the ForeignKey):

Category 1:
  [] Widget 1
  [] Widget 2
  [] Widget 3

Category 2:
  [] Widget 1
  [] Widget 2

How can I achieve this? I have tried using the {% regroup %} template tag to no avail.

Any advice much appreciated.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:53:36+00:00Added an answer on June 16, 2026 at 3:53 am

    You have to write the custom CheckboxSelectMultiple widget. Using the snippet I have tried make the CheckboxSelectMultiple field iterable by adding the category_name as an attribute in field attrs. So that I can use regroup tag in template later on.

    The below code is modified from snippet according to your need, obviously this code can be made more cleaner and more generic, but at this moment its not generic.

    forms.py

    from django import forms
    from django.forms import Widget
    from django.forms.widgets import SubWidget
    from django.forms.util import flatatt
    from django.utils.html import conditional_escape
    from django.utils.encoding import StrAndUnicode, force_unicode
    from django.utils.safestring import mark_safe
    
    from itertools import chain
    import ast
    
    from mysite.models import Widget as wid # your model name is conflicted with django.forms.Widget
    from mysite.models import Feature
    
    class CheckboxInput(SubWidget):
        """
        An object used by CheckboxRenderer that represents a single
        <input type='checkbox'>.
        """
        def __init__(self, name, value, attrs, choice, index):
            self.name, self.value = name, value
            self.attrs = attrs
            self.choice_value = force_unicode(choice[1])
            self.choice_label = force_unicode(choice[2])
    
            self.attrs.update({'cat_name': choice[0]})
    
            self.index = index
    
        def __unicode__(self):
            return self.render()
    
        def render(self, name=None, value=None, attrs=None, choices=()):
            name = name or self.name
            value = value or self.value
            attrs = attrs or self.attrs
    
            if 'id' in self.attrs:
                label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
            else:
                label_for = ''
            choice_label = conditional_escape(force_unicode(self.choice_label))
            return mark_safe(u'<label%s>%s %s</label>' % (label_for, self.tag(), choice_label))
    
        def is_checked(self):
            return self.choice_value in self.value
    
        def tag(self):
            if 'id' in self.attrs:
                self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
            final_attrs = dict(self.attrs, type='checkbox', name=self.name, value=self.choice_value)
            if self.is_checked():
                final_attrs['checked'] = 'checked'
            return mark_safe(u'<input%s />' % flatatt(final_attrs))
    
    class CheckboxRenderer(StrAndUnicode):
        def __init__(self, name, value, attrs, choices):
            self.name, self.value, self.attrs = name, value, attrs
            self.choices = choices
    
        def __iter__(self):
            for i, choice in enumerate(self.choices):
                yield CheckboxInput(self.name, self.value, self.attrs.copy(), choice, i)
    
        def __getitem__(self, idx):
            choice = self.choices[idx] # Let the IndexError propogate
            return CheckboxInput(self.name, self.value, self.attrs.copy(), choice, idx)
    
        def __unicode__(self):
            return self.render()
    
        def render(self):
            """Outputs a <ul> for this set of checkbox fields."""
            return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>'
                    % force_unicode(w) for w in self]))
    
    class CheckboxSelectMultipleIter(forms.CheckboxSelectMultiple):
        """
        Checkbox multi select field that enables iteration of each checkbox
        Similar to django.forms.widgets.RadioSelect
        """
        renderer = CheckboxRenderer
    
        def __init__(self, *args, **kwargs):
            # Override the default renderer if we were passed one.
            renderer = kwargs.pop('renderer', None)
            if renderer:
                self.renderer = renderer
            super(CheckboxSelectMultipleIter, self).__init__(*args, **kwargs)
    
        def subwidgets(self, name, value, attrs=None, choices=()):
            for widget in self.get_renderer(name, value, attrs, choices):
                yield widget
    
        def get_renderer(self, name, value, attrs=None, choices=()):
            """Returns an instance of the renderer."""
    
            choices_ = [ast.literal_eval(i[1]).iteritems() for i in self.choices]
            choices_ = [(a[1], b[1], c[1]) for a, b, c in choices_]
    
            if value is None: value = ''
            str_values = set([force_unicode(v) for v in value]) # Normalize to string.
            if attrs is None:
                attrs = {}
            if 'id' not in attrs:
                attrs['id'] = name
            final_attrs = self.build_attrs(attrs)
            choices = list(chain(choices_, choices))
            return self.renderer(name, str_values, final_attrs, choices)
    
        def render(self, name, value, attrs=None, choices=()):
            return self.get_renderer(name, value, attrs, choices).render()
    
        def id_for_label(self, id_):
            if id_:
                id_ += '_0'
            return id_
    
    class WidgetForm(forms.ModelForm):
        features = forms.ModelMultipleChoiceField(
            queryset=Feature.objects.all().values('id', 'name', 'category__name'),
            widget=CheckboxSelectMultipleIter,
            required=False
        )
        class Meta:
            model = wid
    

    Then in template:

    {% for field in form %}
    {% if field.name == 'features' %} 
        {% regroup field by attrs.cat_name as list %}
    
        <ul>
        {% for el in list %}
            <li>{{el.grouper}}
            <ul>
                {% for e in el.list %}
                    {{e}} <br />
                {% endfor %}
            </ul>
            </li>
        {% endfor %}
        </ul>
    {% else %}
        {{field.label}}: {{field}}
    {% endif %}
    
    {% endfor %}
    

    Results:
    I added countries name in category table, and cities name in features table so in template I was able to regroup the cities (features) according to country (category)

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several checkboxes, each group with only one selectable component. An example of
I have group checkboxes and I like if this group have behaviour like radiobuttons
I Am using jquery 1.6.1 for checking all a group of checkboxes. this is
(I've tried posting this on YUI message group but without any luck) Can anyone
I am using jQuery 1.4.3. I have a group of checkboxes like this: <input
I have multiple groups of checkboxes and radios. Each group has a unique name
I'm trying to create a checkbox group where the option names can be clicked
Trying to work with groupby so that I can group together files that were
I have the following SQLFiddle , i can group the results by time, count
How can I group a number of Dispatcher.Invoke and wait for one group to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.