title: Django: Initializing a FormSet of custom forms with instances
body: <p>Given the following models:</p>
  
  <pre><code>class Graph(models.Model):
      owner = models.ForeignKey(User)
  
      def __unicode__(self):
          return u'%d' % self.id
  
  class Point(models.Model):
      graph = models.ForeignKey(Graph) 
      date  = models.DateField(primary_key = True)
      abs   = models.FloatField(null = True)
      avg   = models.FloatField(null = True)
  
      def __unicode__(self):
          return u'%s' % self.date
  </code></pre>
  
  <p>I am trying to create a form for editing lists of Points.
  The HTML input tags require additional attributes to be set, so I am using the following custom form:</p>
  
  <pre><code>class PointForm(forms.ModelForm):
      graph = forms.ModelChoiceField(queryset = Graph.objects.all(),
                                     widget   = forms.HiddenInput())
      date  = forms.DateField(widget = forms.HiddenInput(), label = 'date')
      abs   = forms.FloatField(widget = forms.TextInput(
                                        attrs = {'class': 'abs-field'}),
                              required = False)
  
      class Meta:
          model  = Point
          fields = ('graph', 'date', 'abs')  # Other fields are not edited.
  
      def pretty_date(self):
          return self.data.strftime('%B')
  </code></pre>
  
  <p>At this point I do not know how to pass instances of the Point class to a FormSet:</p>
  
  <pre><code>def edit(request):
      PointFormSet = forms.formsets.formset_factory(PointForm, extra = 0)
      if request.method == 'POST':
          return
  
      # Receive 3 points to edit from the database.
      graph, res = Graph.objects.get_or_create(id = 1)
      one_day    = datetime.timedelta(days = 1)
      today      = datetime.date.today()
      do_edit    = []
      for date in [today - (x * one_day) for x in range(3)]:
          point, res = Point.objects.get_or_create(graph = graph, date = date)
          do_edit.append(point)
  
      formset = PointFormSet(????) # How is this initialized with the points?
  </code></pre>
  
  <p>I found a hack that somewhat works, but it leads to errors later on when trying to process the resulting POST data:</p>
  
  <pre><code>do_edit = []
  for date in [today - (x * one_day) for x in range(3)]:
      point, res    = Point.objects.get_or_create(graph = graph, date = date)
      data          = point.__dict__.copy()
      data['graph'] = graph
      do_edit.append(data)
  
  formset = PointFormSet(initial = do_edit)
  </code></pre>
  
  <p>How is this done correctly?</p>
  
  <p>For the reference, my template looks like this:</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
  {{ formset.management_form }}
  &lt;table&gt;
      &lt;tbody&gt;
      {% for form in formset.forms %}
          &lt;tr&gt;
              &lt;td&gt;{{ form.graph }} {{ form.date }} {{ form.pretty_date }}:&lt;/td&gt;
              &lt;td width="100%"&gt;{{ form.abs }}&lt;/td&gt;
          &lt;/tr&gt;
      {% endfor %}
      &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>