title: Django: How to get the selected value from a custom widget?
body: <p>I'm writing a custom Form widget that overrides the <a href="https://github.com/django/django/blob/stable/1.5.x/django/forms/models.py#L925" rel="nofollow"><code>choice</code> method in <code>django.forms.models.ModelChoiceIterator</code></a>:</p>
  
  <pre><code>class CustomIterator(ModelChoiceIterator):                                                                                                                                                                                                                                       
      def choice(self, obj):                                                       
          return (self.field.prepare_value(obj),                                   
              self.field.label_from_instance(obj), obj) 
  </code></pre>
  
  <p>As well as the <a href="https://github.com/django/django/blob/stable/1.5.x/django/forms/models.py#L979" rel="nofollow"><code>_get_choices</code> method on <code>django.forms.models.ModelChoiceField</code></a>:</p>
  
  <pre><code>class CustomField(ModelChoiceField):                                                
      def _get_choices(self):                                                      
          if hasattr(self, '_choices'):                                            
              return self._choices                                                 
          return ElfIterator(self)                                                 
      choices = property(_get_choices, ChoiceField._set_choices)
  </code></pre>
  
  <p>(I followed the example on <a href="http://srcmvn.com/blog/2013/01/15/django-advanced-model-choice-field/" rel="nofollow">this blog post</a>)</p>
  
  <p>I need to create a totally custom widget that selects objects based on, say, the value of the <code>data-selected</code> attribute on an HTML element. I've been able to get the custom HTML/styling to be displayed on the form using the instance attributes added by the above: </p>
  
  <pre><code>from django.template.loader import render_to_string
  
  class CustomWidget(Widget):                                                                                                        
  
      def render(self, name, value, attrs=None):                                                                                                                                                                                                                 
          obj_list = [item[2] for item in self.choices]                                                                                 
          obj_dict = [model_to_dict(obj) for obj in obj_list]                                                                         
          output = render_to_string('myapp/widgets/custom_widget.html',                                                              
              { 'obj_dict': obj_dict })                                                                                                  
          return mark_safe(output) 
  </code></pre>
  
  <p>Now I'm attempting to override the <code>value_from_datadict</code> method on this same class, however it's not clear to me, even from reading the source code, how I'd be able to return the selected value based on an arbitrary HTML attribute without a Select widget. </p>