title: Getting model ContentType in migration - Django 1.7
body: <p>I have a data migration that updates some permissions. I know there are some known issues with permissions in migrations and i was able to avoid some trouble by creating the permissions in the migration it self (rather then using the tuple shortcut in the model).</p>
  
  <p>The migration :</p>
  
  <pre><code>from __future__ import unicode_literals
  from django.db import migrations, models
  from django.conf import settings
  
  def create_feature_groups(apps, schema_editor):
      app = models.get_app('myauth')
  
      Group = apps.get_model("auth", "Group")
      pro = Group.objects.create(name='pro')
  
      Permission = apps.get_model("auth", "Permission")
      ContentType = apps.get_model("contenttypes", "ContentType")
      invitation_contenttype = ContentType.objects.get(name='Invitation')
  
      send_invitation = Permission.objects.create(
           codename='send_invitation',
           name='Can send Invitation',
           content_type=invitation_contenttype)
  
      pro.permissions.add(receive_invitation)    
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('myauth', '0002_initial_data'),
      ]
  
      operations = [
              migrations.RunPython(create_feature_groups),
      ]
  </code></pre>
  
  <p>After some trial and error i was able to make this work using <code>manage.py migrate</code> but i'm getting errors in the test <code>manage.py test</code>. </p>
  
  <pre><code>__fake__.DoesNotExist: ContentType matching query does not exist.
  </code></pre>
  
  <p>Debugging a bit discovered that there are no <code>ContentType</code> at this point in the migration when run in test (not sure why). Following the advice in this <a href="http://andrewingram.net/2012/dec/common-pitfalls-django-south/#contenttypes-and-permissions" rel="noreferrer">post</a> i tried updating the content types manually in the migration it self. Added : </p>
  
  <pre><code>from django.contrib.contenttypes.management import update_contenttypes
  update_contenttypes(app, models.get_models())
  </code></pre>
  
  <p>before fetching the content type for the <code>Invitation</code> model. Got the following error </p>
  
  <pre><code>  File "C:\Python27\lib\site-packages\django-1.7-py2.7.egg\django\contrib\contenttypes\management.py", line 14, in update_contenttypes
      if not app_config.models_module:
  AttributeError: 'module' object has no attribute 'models_module'
  </code></pre>
  
  <p>There must be <strong>some</strong> way to create/update permissions in data migrations in a testable way.</p>
  
  <p>Thanks. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Finally made it work by adding</p>
  
  <pre><code>from django.contrib.contenttypes.management import update_all_contenttypes
  update_all_contenttypes() 
  </code></pre>
  
  <p>oddly enough this one was not sufficient</p>
  
  <pre><code>update_contenttypes(apps.app_configs['contenttypes'])
  </code></pre>
  
  <p>I would love to know why all of this is necessary</p>