title: Check prefiled formset and remove empty
body: <p>On my reservation site for an event, I want to allow people to manage their guests list. I represent the reservation with a <code>Registration</code> model and the guests with a <code>Guest</code> model using a foreign key <code>invited = models.ForeignKey(Registration)</code>.</p>
  
  <p>I used a <code>modelformset_factory</code> to create a set of forms to record each guest at the registration. Now, to update this list, I use the following code:</p>
  
  <pre><code>registr = Registration.objects.get(id=postid) # get the registration
  GuestFormSet = modelformset_factory(Guest,
                         extra=MAXGUESTS,
                         exclude=('invited',)) # generate MAXGUESTS Guest forms
  guests = GuestFormSet(request.POST) # put the submited formset
  if guests.is_valid():
      guests = guests.save(commit=False)
      for guest in guests:
          guest.invited = registr
          guest.save()
  </code></pre>
  
  <p>This works (+/-) to update the fields of an existing guest or to ass one but now I want to have the possibility to <em>delete</em> guest (simply by emptying the form of the guest). The problem is that I have an error "This field is required." for each field of the guest I want to remove.</p>
  
  <p>Any idea how I can do that ?</p>
  
  <p>Thank you</p>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>registr = Registration.objects.get(id=postid) # get the registration
  maxg = max(0,MAXGUESTS - len(Guest.objects.filter(invited=registr))) # MAXGUEST form, existing guests included
  GuestFormSet = modelformset_factory(Guest,
                         extra=maxg,
                         can_delete=True,
                         exclude=('invited',)) # generate MAXGUESTS Guest forms
  guests = GuestFormSet(request.POST) # put the submited formset
  
  if guests.is_valid():
      # create new guests
      guests = guests.save(commit=False)
      for guest in guests:
          guest.invited = registr
          guest.save()
  
      # get the guests from the updated database
      maxg = max(0,MAXGUESTS - len(Guest.objects.filter(invited=registr)))
      GuestFormSet = modelformset_factory(Guest, extra=maxg, can_delete=True, exclude=('invited',))
      guests = GuestFormSet(queryset=Guest.objects.filter(invited=registr))
  </code></pre>