title: adding formset like extra fields to a django ModelForm admin
body: <p>based on my before problem: <a href="https://stackoverflow.com/questions/8142493/add-an-object-along-with-its-related-objects-in-the-same-form-in-django-admin/8144010#8144010">add an object along with it&#39;s related objects in the same form in django admin</a>
  now I've decided to add some extra fields and a formset to my modeladmin in admin,is it possible to add a formset like other fields to my modeladmin?what's wrong with my code?</p>
  
  <p>edit:
  i have these models:</p>
  
  <pre><code>class Gallery(models.Model):
     HeadImage = models.ImageField(upload_to="gallery") 
  
  class Image(models.Model):
     Image = models.ImageField(upload_to="gallery")
     Gallery = models.ForeignKey(Gallery, related_name='images',blank=True)
  
  class Agency(models.Model):
     # some fields ...
     Gallery=models.ForeignKey(Gallery)
  </code></pre>
  
  <p>as U can see,each Agency have a HeadImage with n optional images,now I wanna change Agency form in admin to handle all of these models at the same form,I mean Agency with a fileinput for HeadImage and a formset for Image model.I did this to override my ModelAdmin form.this is the code:</p>
  
  <pre><code>class AgencyAdminForm(forms.ModelForm):
     HeadImage=forms.FileField(required=True)
     ImageFormSet = inlineformset_factory(Gallery,Image, can_delete=True,extra=4)   
     class Meta:
        model = Agent
     def __init__(self, *args, **kwargs):
        super(AgencyAdminForm, self).__init__(*args, **kwargs)
        if kwargs.has_key('instance'):
           instance = kwargs['instance']
           self.initial['HeadImage'] = instance.HeadImage
           self.initial['ImageFormSet'] = ImageFormSet(request.POST, request.FILES, instance=Gallery())
     def save(self, commit=True):
        model = super(AgencyAdminForm, self).save(commit=False)
        gallery=Gallery.objects.create(HeadImage=self.cleaned_data['HeadImage'])
        formset=self.ImageFormSet(request.POST, request.FILES, instance=gallery)
        image=formset.save()
        model.Gallery=gallery
        if commit:
           model.save()
        return model   
  
  class AgencyAdmin(admin.ModelAdmin):
      form = AgencyAdminForm
  
  admin.site.register(Agency,AgencyAdmin)
  </code></pre>
  
  <p>my code cause adding inputfile for HeadImage,but it doesn't add formset to admin form.my problem is that how to handle a formset like other extra fields.</p>