title: Django unzip a file, add contents to database
body: <p>I am trying to create a system that enables the admin to upload a zipfile, then the script will automatically, using <code>signals</code>, unzip it, search for all the files in <code></code> <code>jpg,png</code>. create a <code>list</code>  of them and generate a database record according to it.</p>
  
  <p>In models, i have <code>Project</code> and <code>Photo</code> table, <code>Photo</code> has <code>Many-to-One</code> aka <code>Foreign Key</code> relationship with <code>Project</code>.</p>
  
  <p>The script below is with the signal i am working. I can get <code>instance.file_zip.path</code> without errors, and the script works well when run manually.</p>
  
  <p>Long-time debugging and I assume that there is something wrong with <code>belongs_to=instance</code> but I do not know how to fix it as I didn't actually understand why it gaves an error.
  The extraction part works fine, I just put them here for reference, most likely you will not need to read and understand it.</p>
  
  <pre><code>@receiver(post_save, sender=Project)
  def unzip_and_process(sender, instance, **kwargs):
      #project_zip = FieldFile.open(file_zip, mode='rb')
      file_path = instance.file_zip.path
      file_list = []
      with zipfile.ZipFile(file_path, 'r') as project_zip:
          project_zip.extractall(re.search('[^\s]+(?=\.zip)', file_path).group(0))
          project_zip.close()
      for root, dirs, files in os.walk(file_path):
          for filename in files:
              file_list.append(os.path.join(root, filename))
      photo_list = filter(filter_photos, file_list)
      for photo in photo_list:
          print 'Processing %s'%photo
          p = Photo.objects.create(belongs_to=instance, img=photo, desc='Processed from zipfile')
          p.save()
  </code></pre>
  
  <p><strong>update</strong></p>
  
  <pre><code>class Project(models.Model):
      project_name=models.CharField(max_length=150)
      date_taken=models.DateField()
      date_deadline=models.DateField()
      price=models.FloatField()
      price_paid=models.BooleanField()
      owner=models.ForeignKey(User)
      file_zip=models.FileField(upload_to='projects/%Y/%m/%d')
  
      def __unicode__(self):
          return self.project_name
  
      def file_path(self):
          return re.search('[^\s]+(?=\.zip)', self.file_zip.name).group(0)
  
  class Photo(models.Model):
  
      def project_path(instance, filename):
          return '%s/%s'%(instance.belongs_to.file_path(),filename)
  
      belongs_to=models.ForeignKey(Project, verbose_name="related_project")
      img=models.ImageField(upload_to=project_path, max_length=255)
      desc=models.CharField(max_length=255)
  
      def __unicode__(self):
          return '%s FROM [%s]'%(self.img.name,self.belongs_to)
  </code></pre>