title: Create bulk database entries efficiently?
body: <p>I'm trying to create an <code>Activty</code> object for a large (300+ at a time) list of <code>Inquiry</code> objects.  I have a single <code>ModelForm</code> which is being posted back, and I need to create seperate instances, and attach them to my <code>Inquiry</code> via a <code>GenericForeignKey</code>.  Let's get to some code:</p>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class InquiryEntry(models.Model):
      content_type = models.ForeignKey(ContentType)
      object_id = models.PositiveIntegerField() 
      entry = generic.GenericForeignKey('content_type', 'object_id')
  
  class Inquiry(models.Model):
      entries = models.ManyToManyField('InquiryEntry')
      # And many more fields.
      def add_entry(self, obj):
          entry = self.entries.create(entry=obj)
          self.save()
          return entry
  
  class Activity(models.Model):  
      ts = models.DateTimeField(auto_now_add=True)                  
      due_date = models.DateField(auto_now=False)
      ## And many more fields.
  </code></pre>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>def bulk_create_activities(request):
      activity_form = ActivityForm()
      if request.method == "POST":
          activity_form = ActivityForm(request.POST)
          if activity_form.is_valid():    
              pks = [int(x) for x in request.POST.get('pks', '').split(',')]
              for inquiry in Inquiry.objects.filter(pk__in=pks):
                  instance = ActivityForm(request.POST).save()
                  inquiry.add_entry(instance)     
                  inquiry.save()  
  </code></pre>
  
  <p>What I am looking for is a way to insert these into the database, preferably in one pass so that the request can be processed faster.  I prefer not to drop to the database level as this application is deployed across multiple database vendors, but if that is the only way to proceed, so be it (examples for MySQL and Postgres would be awesome).</p>
  
  <hr>
  
  <p><strong>Note:</strong> I know that there is a <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create" rel="nofollow"><code>bulk_create</code></a> in the development version, but that is out of the question until there is a stable release.</p>