title: Django Save Multiple Objects At Once
body: <p>I have been practicing Django for a while now. Currently I am using it in a project where I'm fetching Facebook data via GET requests and then saving it to an sqlite database using Django models. I would like to know how can I improve the following code and save a list of Facebook posts and their metrics efficiently. In my current situation, I am using a <code>for</code> loop to iterate on a list containing several Facebook Posts and their respective metrics which is then associated to the specific Django model and finally saved.</p>
  <pre class="lang-py prettyprint-override"><code>
  def save_post(post_id, page_id):
  
      facebook_post = Post(post_id=post_id,
                      access_token=fb_access_token)
  
      post_db = PostsModel(page_id=page_id, post_id=post.post_id)
      post_db.message = facebook_post.message
      post_db.story = facebook_post.story
      post_db.full_picture = facebook_post.full_picture
      post_db.reactions_count = facebook_post.reactions_count
      post_db.comments_count = facebook_post.comments_count
      post_db.shares_count = facebook_post.shares_count
      post_db.interactions_count = facebook_post.interactions_count
      post_db.created_time = facebook_post.created_time
      post_db.published = facebook_post.published
      post_db.attachment_title = facebook_post.attachment_title
      post_db.attachment_description = facebook_post.attachment_description
      post_db.attachment_target_url = facebook_post.attachment_target_url
      post_db.save()
  
  </code></pre>
  <p><code>post_db</code> is a Django model object instantiated using <code>PostsModel</code> while <code>Post</code> is a normal Python Class which I wrote. The latter is simply a collection of GET requests which fetches data from Facebook's Graph API and returns JSON data whereby I associate relevant data to class attributes (<code>message</code>, 'shares_count`).</p>
  <p>I read about the <code>bulk_create</code> function from Django's documentation but I don't know how to pass on the above. I also tried using <code>multiprocessing</code> and <code>Pool</code> but the above function does execute. Right now, I am just iterating sequentially on a list. As the list increases in length, it takes more time to save.</p>
  <pre class="lang-py prettyprint-override"><code>
  def create(self, request):
          page_id = request.data['page_id']
  
          page = get_object_or_404(PagesModel, pk=page_id)
          post_list = get_list_or_404(PostsModel, page_id=page_id)
  
          for post_id in post_list:
              save_post(post_id=post_id, page_id=page)
  </code></pre>
  <p>The above function gets an already saved list from the database for a specific page based on the <code>page_id</code>. Then, the <code>for</code> loop iterates on each post in the list and its <code>post_id</code> and <code>page</code> instance are sent to the <code>save_post</code> function to fetch its data and save it.</p>
  <p>Huge thanks if anyone can suggest a more effective way to tackle this. Thank you.</p>