3

I have model with ManyToManyField. Now I need form but don't need select field in template.

class Foo(models.Model):
    name = models.CharField(max_length=50)
    short_description = models.CharField(max_length=100)
    price = models.IntegerField()

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    foo = models.ManyToManyField(Foo, blank=True, null=True, related_name='foos')

    def __unicode__(self):
        return unicode(self.id)

What I really need is to display all data from the Foo model with checkboxes in template, instead of select field which I have if use model.Form and {{ form }} call in template.

class BarForm(forms.ModelForm):
    class Meta:
        model = Bar

view.py

def show_form(request, id):
    foo = get_object_or_404(Foo, id=id)
    form = BarForm()
    ...
Goran
  • 6,644
  • 11
  • 34
  • 54

1 Answers1

4

To show a ManyToManyField as checkboxes instead of a select field, you need to set the widget in the Meta class of the appropriate ModelForm subclass. Then to show a custom label on each checkbox, create your own form field class derived from ModelMultipleChoiceField, and override label_from_instance. You may also add HTML tags to the string returned by label_from_instance to make it look as pretty as you want, but remember to wrap the returned string with mark_safe.

from django.forms.widgets import CheckboxSelectMultiple
from django.forms.models import ModelMultipleChoiceField
...
class CustomSelectMultiple(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s: %s %s" %(obj.name, obj.short_description, obj.price)

class BarForm(forms.ModelForm):
    foo = CustomSelectMultiple(queryset=Foo.objects.all())
    class Meta:
        model = Bar
        widgets = {"foo":CheckboxSelectMultiple(),}
Simon Kagwi
  • 1,636
  • 2
  • 19
  • 25
  • Thanks Simon. My mistake when I was translate my real model names to the foo/bar model names. Actually it's 'BarForm'. I just trying to use 'CheckboxSelectMultiple' and I have checkboxes with unicode names. How to get all objects data in template? Name, short_description and price with checkbox? – Goran Dec 25 '11 at 19:52
  • I've edited my answer to use correct class/variable names and also added info on how to customize the label on each checkbox (it would be good if you'd edit your question as well to avoid confusion). I hope it helps! – Simon Kagwi Dec 25 '11 at 20:21
  • Seems that I'm lost Checkboxes in template using this code. Now I have all data but in select field, not as checkboxes. ` widgets = {"foo":CheckboxSelectMultiple(),} ` don't work anymore. – Goran Dec 25 '11 at 21:44
  • Fixed! I'm remove `widgets = {"foo":CheckboxSelectMultiple(),}` line and change `foo = CustomSelectMultiple(queryset=Foo.objects.all())` to `foo = CustomSelectMultiple(widget=forms.CheckboxSelectMultiple, queryset=Foo.objects.all())` thanks to Yuji Tomita comment. Thanks! – Goran Dec 25 '11 at 22:24
  • I tried all these suggestions but no joy! Tried in Django: 1.5.4 – disruptive Nov 01 '13 at 19:25