title: Django: Accessing model fields from form widget in template
body: <p>I have a ModelForm which uses a CheckboxSelectMultiple widget to represent data from a ManyToManyField.  By default this will render a list of checkboxes with the label being set to the return value of the <code>__str__</code> method of the related model.  However, I wish to display data from other fields in the related model in addition to the choice_label provided by the widget.  For example consider the implementation below:</p>
  
  <p>models.py</p>
  
  <pre><code>from django.db import models
  
  class Category(models.Model):
      name = models.CharField(max_length=50)
  
      def __str__(self):
          return self.name
  
  class Item(models.Model):
      name = models.CharField(max_length=100)
      category = models.ForeignKey('Category', on_delete=models.CASCADE)
  
      def __str__(self):
          return self.name
  
  class Order(models.Model):
      items = models.ManyToManyField('Item')
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>from django import forms
  from .models import Order
  
  class OrderForm(forms.ModelForm):
      class Meta:
          model = Order
          fields = ['items']
          widgets = {'items': forms.widgets.CheckboxSelectMultiple}
  </code></pre>
  
  <p>template.html</p>
  
  <pre><code>{% for item in form.items %}
      &lt;tr&gt;
          &lt;td&gt;{{ item.tag }}&lt;/td&gt;
          &lt;td&gt;{{ item.choice_label }}&lt;/td&gt;
          &lt;td&gt;{{ item.category }}&lt;/td&gt;
      &lt;/tr&gt;
  {% endfor %}
  </code></pre>
  
  <p>The problem here is the <code>{{ item.category }}</code> part of the template.html.  When looping through all item checkboxes with <code>{% for item in form.items %}</code>, <code>item</code> is of type <code>BoundWidget</code>, not <code>Item</code>.  This is what provides the <code>tag</code> and <code>choice_label</code> attributes for rendering the checkbox for the form.  My problem is that <code>BoundWidget</code> doesn't appear to have any reference back to the <code>Item</code> instance that the checkbox represents that would allow me to access any of the additional model data such as the category the item is in.  Is there any way around this?</p>
  
  <p>EDIT:</p>
  
  <p>The only way I can think to do this is the following:</p>
  
  <p>templatetags/app_tags.py</p>
  
  <pre><code>from django import template
  from models import Item
  
  register = template.Library()
  
  @register.inclusion_tag('app/order_item_table.html')
  def show_item_table(items):
      item_objs = []
      for item in items:
          id = item.data['value']
          obj = Item.objects.get(id=id)
          item_objs.append((item, obj))
      return {'items': item_objs}
  </code></pre>
  
  <p>order_item_template.html</p>
  
  <pre><code>{% for item, obj in items %}
      &lt;tr&gt;
          &lt;td&gt;{{ item.tag }}&lt;/td&gt;
          &lt;td&gt;{{ item.choice_label }}&lt;/td&gt;
          &lt;td&gt;{{ obj.category }}&lt;/td&gt;
      &lt;/tr&gt;
  {% endfor %}
  </code></pre>
  
  <p>template.html</p>
  
  <pre><code>{% load app_tags %}
  
  {% show_item_table form.items %}
  </code></pre>
  
  <p>But this seems incredibly hacky, I would really like to know if there is a better way to do this?</p>