user2142291
user2142291

Reputation: 133

django modelmultiplechoicefield and widget checkboxselectmultiple

I would like to have something like the admin interface.

here is the code for the form:

class NewRoleFrom(forms.Form):
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )

So, it's simple, I have the Role label(Role:) then each role in the database is rendered with a checkbox. Like that I can get back all role objects choosen by the user. But at the begining of each line I have a bullet, how can I remove it? Then is it possible to add on each others attribute like when we define a list_display in the admin.py ?

Upvotes: 4

Views: 7932

Answers (3)

burroughs06
burroughs06

Reputation: 353

I would override the CheckboxSelectMultiple class with a custom class and insert the style changes directly on the render output. See code below

class CustomCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
    def __init__(self, attrs=None):
        super(CustomCheckboxSelectMultiple, self).__init__(attrs)

    def render(self, name, value, attrs=None, choices=()):
        output = super(CustomCheckboxSelectMultiple, self).render(name, value, attrs, choices)

        style = self.attrs.get('style', None)
        if style:
            output = output.replace("<ul", format_html('<ul style="{0}"', style))

        return mark_safe(output)

And then in your form:

class NewRoleFrom(forms.Form):
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.all(),
        widget=CustomCheckboxSelectMultiple(attrs={'style': 'list-style: none; margin: 0;'})
    )

Upvotes: 0

ken
ken

Reputation: 29

in your form template just iterate over the roles

form.html

{% for role in form.role %}
    <div class="checkbox">
      {{ role }}
    </div>
{% endfor %}

then pretty up with css.

Upvotes: 2

Ricola3D
Ricola3D

Reputation: 2442

Here is the source for that widget from django.forms.widgets module :

class CheckboxSelectMultiple(SelectMultiple):
    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])
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            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 = 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>')
        return mark_safe(u'\n'.join(output))

    def id_for_label(self, id_):
        # See the comment for RadioSelect.id_for_label()
        if id_:
            id_ += '_0'
        return id_

You can see the bullets are due to django CSS for lists. So to remove them, think about creating a new wudget inheriting from CheckboxSelectMultiple with adding a class to the "ul" html tag, and then add your own css with the solution detailed here.

Upvotes: 0

Related Questions