title: Celery Deadlock when saving data in Django, why?
body: <p>I have a <strong>Deadlock</strong> when using a <strong>Celery</strong> task to save new customers from a CSV. This is what I have working so far.</p>
  
  <pre><code>  for line in csv.reader(instance.data_file.read().splitlines()):
          for index, item in enumerate(line):
              number = int(item)
              # TODO: Turn into task
              Customer.objects.create_customer(
                  mobile=number,
                  campaign=instance.campaign,
                  reward_group=instance.reward_group,
                  company=instance.company,
              )
  </code></pre>
  
  <p>No errors.</p>
  
  <p>However, when add this same code to a Celery task I get the following error...</p>
  
  <blockquote>
    <p>Deadlock found when trying to get lock; try restarting transaction'</p>
  </blockquote>
  
  <p>So, this leads me to believe that I have done something wrong with my celery setup here. Can anyone spot what?</p>
  
  <p>Here is the new Celery task that gives the deadlock error. I'm using <code>shared_task</code> as these task will at some point run on a different machine without Django, but that should not matter for now.</p>
  
  <p>The first row in the CSV import ok, then I get a deadlock error...</p>
  
  <pre><code>for line in csv.reader(instance.data_file.read().splitlines()):
      for index, item in enumerate(line):
          number = int(item)
          celery_app.send_task('test.tasks.create_customer_from_import', args=[number, instance.id], kwargs={})
  </code></pre>
  
  <p><strong>tasks.py</strong></p>
  
  <pre><code># Python imports
  from __future__ import absolute_import
  
  # Core Django imports
  from celery import shared_task
  
  from mgm.core.celery import app as celery_app
  
  @shared_task
  def create_customer_from_import(number, customer_upload_id):
      customer_upload = CustomerUpload.objects.get(pk=customer_upload_id)
      new_customer = Customer.objects.create_customer(
          mobile=number,
          campaign=customer_upload.campaign,
          reward_group=customer_upload.reward_group,
          company=customer_upload.company,
      )
      return new_customer
  </code></pre>
  
  <p><strong>celery.py</strong></p>
  
  <pre><code>from __future__ import absolute_import
  
  import os
  
  from celery import Celery
  
  from django.conf import settings
  
  # set the default Django settings module for the 'celery' program.
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test.settings')
  
  app = Celery('test-tasks')
  
  # Using a string here means the worker will not have to
  # pickle the object when using Windows.
  app.config_from_object('django.conf:settings')
  app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
  </code></pre>
  
  <p><strong>This is the CustomerManager:</strong></p>
  
  <pre><code>class CustomerManager(models.Manager):
      def create_customer(self, mobile, campaign, reward_group, company, password=None):.
          user = AppUser.objects.create_user(mobile=mobile)
          # Creates a new customer for a company and campaign
          customer = self.model(
              user=user,
              campaign=campaign,
              reward_group=reward_group,
              company=company
          )
  
          customer.save(using=self._db)
  </code></pre>