title: django field/widget for multiple values
body: <p>I seriously can't figure out how to manage to do this.</p>
  
  <p>I would like to use django forms to validate the following (unknown number of <code>aname</code>):</p>
  
  <pre><code>&lt;input type="hidden" name="aname" value="someJSONdump1"/&gt;
  &lt;input type="hidden" name="aname" value="someJSONdump2"/&gt;
  &lt;input type="hidden" name="aname" value="someJSONdump3"/&gt;
  &lt;input type="hidden" name="aname" value="someJSONdump4"/&gt;</code></pre>
  
  <p>and on the django side, I'm calling:</p>
  
  <pre><code>form = myforms.MyForm(request.POST, request.FILES)
  if (form.is_valid()):
      # do something
  else:
      # redisplay the form</code></pre>
  
  <p>How do I define <code>MyForm</code> to allow me to validate each <code>aname</code> and also, when in error, the widget to redisplay the above <code>&lt;input&gt;s</code>?</p>
  
  <p>I can't figure out how to use the <a href="https://docs.djangoproject.com/en/dev/ref/forms/fields/#multivaluefield" rel="nofollow">MultiValueField</a> or even if it's the right thing to use. It seems to be a solution when you know how many fields you have?</p>
  
  <p>Using <code>clean_aname()</code> in the form is no help as <code>self.cleaned_data.get('aname')</code> is only the last value.</p>
  
  <p>Without the form, I would use something like <code>request.POST.getlist('aname')</code>, but I would like to avoid this if I can do it with <code>django.forms</code>.</p>
  
  <p>Thanks for your help.</p>
  
  <hr>
  
  <p><b><strong><em>EDIT</em></strong></b></p>
  
  <p>I've left aside that I was defining more fields from a ModelForm. I think this might have some effects with formset. Here is where I am at... Is this solution considered to be "Django forms" compatible?</p>
  
  <pre><code>class MyField(forms.Field):
      widget = MyWidget
  
      def to_python(self, value):
          if (isinstance(value, basestring)):
              value = [value]
          return [json.loads(v) for v in value]
  
  class MyForm(forms.ModelForm):
      class Meta:
          model = models.MyModel
  
      aname = MyField()
  
      def clean(self):
          cleaned_data = super(MyForm, self).clean()
          cleaned_data['aname'] = self.fields['aname'].clean(self.data.getlist('aname'))
          return cleaned_data</code></pre>
  
  <p>Now, I have to define <code>MyWidget</code> to allow me to display a list of <code>&lt;input type="hidden"&gt;</code>, but I would like to know if this solution sound acceptable. Maybe I could have done this in <code>clean_aname()</code> too.</p>