title: dynamically populating fields in a formset
body: <p>I have two models that looks like this:</p>
  
  <pre><code>class RouteBase(models.Model):
      base        =   models.ForeignKey("Base")
      route       =   models.ForeignKey("Route")
      sequence    =   models.IntegerField()
  
  class Route(models.Model):
      bases       =   models.ManyToManyField("Base", through="RouteBase", blank=True)
      description =   models.TextField(blank=True)
      #and a few other attributes omitted for brevity
  </code></pre>
  
  <p>...then a modelform that looks like this:</p>
  
  <pre><code>class RouteBaseForm(ModelForm):
      base = forms.ModelChoiceField(queryset=Base.objects.all(), widget=forms.TextInput)
      sequence = forms.IntegerField(widget=forms.HiddenInput)
  
      class Meta:
          model = RouteBase
  </code></pre>
  
  <p>As you can see, the sequence widget is hidden, as I want this field to be automatically handled by django. I want the user to have to just enter the Base via a text box. The sequence is inferred by the order of the textboxes.</p>
  
  <p>I have created a formset with this form for creating/editing all the bases in the route:</p>
  
  <pre><code>RouteBaseFormset = inlineformset_factory(Route, RouteBase, form=RouteBaseForm, extra=5, )
  </code></pre>
  
  <p>When this formset is created, the sequence field is empty. I need to fill it in with values before I save the formset (else it won't validate). I can think of about 4 ways to go about this</p>
  
  <ol>
  <li>Right before I send the formset off to the template, I run this code:</li>
  </ol>
  
  <pre>
      i=1
      for form in formset.forms:
          form.fields["sequence"].initial = i
          i += 1
  </pre>
  
  <p>This works fine, except for one problem. When the formset gets submitted back to the view for saving, <strong>all</strong> 5 extra fields that have been created with the formset are filled in with a sequence value. This causes problems when the user only wants to add 2 or 3 bases to the route. Validation errors popup because the required field "base" is empty for that form. I <em>could</em> run a bit of code after the formset has been POSTed that checks to see if a base is present, if not, remove the sequence, but if I'm going to do that, I may as well...</p>
  
  <ol start="2">
  <li><p>Run a bit of code when the formset is POSTed that checks to see if a Base has been entered, if so, add a sequence, if not, then leave that field blank. That way when you try to <code>.save()</code> the formset, the empty values ensure that that particular form is not put into the database. The only problem is that I can't do anything to the form until I run <code>.save(commit=False)</code> which I can't do because the formset doesn't validate. But I could...</p></li>
  <li><p>Add the sequence values by copying the <code>request.POST</code> variable and manually setting the sequence, but that seems awfully hacky.</p></li>
  <li><p>I could also just remove <code>blank=True</code> from one of my <code>RouteBase</code> fields, but I don't really want to do that.</p></li>
  </ol>
  
  <p>So what should I do here?</p>