django: customizing display of ModelMultipleChoiceField

django, django-forms

Solution

The field class has a method `label_from_instance` that controls how the object is represented. You can overwrite it in your own field class:

from django.forms.models import ModelMultipleChoiceField

class MyMultipleModelChoiceField(ModelMultipleChoiceField):

    def label_from_instance(self, obj):
        return "%s | %s" % (obj.name, obj.field1)

You should also be able to output some html with that...

Problem

ModelMultipleChoiceField is displayed in a template is a list of checkboxes with unicode of representation of corresponding objects. How do I display ModelMultipleChoiceField in table form with arbitrary fields in arbitrary columns? For example: [x] | obj.name | obj.field1

Original source