title: Django ModelChoiceField optgroup tag
body: <p>How can I  set in ModelChoiceField <strong>optgroup</strong> tag?</p>
  
  <p>This is example:</p>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Link(models.Model):
      config = models.ForeignKey(Config)
      name = models.URLField(u'Name', null=True, max_length=50)
      gateway = models.IPAddressField(u'Gateway', null=True)
      weight = models.IntegerField(u'Weight', null=True)
      description = models.TextField(u'Description', blank=True)
  
  def __unicode__(self):
      return self.name
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class LinkForm(ModelForm):
      config = ModelChoiceField(queryset=Config.objects.all(), empty_label="Choose a link",widget=GroupedSelect())
  
  class Meta:
      model = Link
  </code></pre>
  
  <p>I would like to render my ChoiceField like this:</p>
  
  <p><strong>example.html</strong></p>
  
  <pre><code>&lt;select id="id_config" name="config"&gt;
      &lt;option selected="selected" value=""&gt;Choose a link&lt;/option&gt;
      &lt;optgroup label="Configuration" &gt;
          &lt;option value="8"&gt;Address: 192.168.1.202/255.255.255.0 &lt;/option&gt;
          &lt;option value="9"&gt;Address: 192.168.1.240/255.255.255.0 &lt;/option&gt;
          &lt;option value="10"&gt;Address: 192.168.3.1/255.255.255.0 &lt;/option&gt;
      &lt;/optgroup&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><em><strong></em>**UPDATE*<em>*</em></strong></p>
  
  <h2>I solved my problem like this:</h2>
  
  <pre><code>class GroupedSelect(Select):
      def render(self, name, value, attrs=None, choices=()):
          if value is None: value = ''
          final_attrs = self.build_attrs(attrs, name=name)
          output = [format_html('&lt;select{0}&gt;', flatatt(final_attrs))]
          for index, option_gp in enumerate(self.choices):
              if index == 0:
                  option_value = smart_unicode(option_gp[0])
                  option_label = smart_unicode(option_gp[1])
                  output.append(u'&lt;option value="%s"&gt;%s&lt;/option&gt;' %  (escape(option_value), escape(option_label)))
                  output.append('&lt;optgroup label = "Configuration"&gt;')
              elif index!=0 and index &lt;= len(self.choices):
                  option_value = smart_unicode(option_gp[0])
                  option_label = smart_unicode(option_gp[1])
                  output.append(u'&lt;option value="%s"&gt;%s&lt;/option&gt;' % (escape(option_value), escape(option_label)))          
          output.append(u'&lt;/optgroup&gt;')
          output.append(u'&lt;/select&gt;')
          return mark_safe('\n'.join(output))
  </code></pre>