title: Django: How to add dynamic classes to select options in a form
body: <p>I have a ModelForm that I'm rendering in the template with django-widget-tweaks. I have a ChoiceField select that prints out something like this: </p>
  
  <pre><code>&lt;select&gt;
      &lt;option value="object.id"&gt;object.name&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>But for Javascript manipulation purposes, I need the following:</p>
  
  <pre><code>&lt;select&gt;
      &lt;option value="object.id" class="object.otherPropery"&gt;object.name&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p><a href="https://pypi.python.org/pypi/django-widget-tweaks#add-class" rel="nofollow">django-widget-tweaks has an add_class function</a>, but it only adds a class to the select box, not the contained options.</p>
  
  <p>I have also tried rolling my own select box:</p>
  
  <pre><code>&lt;select&gt;
      {% for object in form.object_field %}
          &lt;option value="{{ object.id }}" class="{{ object.otherProperty }}"&gt;{{ object.name }}&lt;/option&gt;
      {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>But when the form does not validate and errors are returned, the option the user had selected previously gets reset back to the default (first) option (this <em>does</em> happen with django-widget-tweaks).</p>
  
  <p>I have also tried overriding the <code>render_option</code> and <code>render_options</code> fields of the Select widget, but the additional properties of the object are not being passed to those functions.</p>
  
  <p>I feel like there's a simple solution for what I'm trying to do. Any help would be much appreciated! </p>