6

I’m trying to migrate a few models from one Django app to another and based on this question How do I migrate a model out of one django app and into a new one? I’ve got it pretty much worked but when creating the first migration I’m getting this error:

"The model 'contenttype' from the app 'contenttypes' is not available in this migration."

Google and SO doesn’t seem to find any cases for this happening and the aforementioned question doesn’t have anything specific to say about it either, except the comment in the code:

if not db.dry_run:
    # For permissions to work properly after migrating
    orm['contenttypes.contenttype'].objects.filter(app_label='common', model='cat').update(app_label='specific')

Would really appreciate any insight into what am I doing wrong.

Here are the two migration files:

Create:

# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

    def forwards(self, orm):
        db.rename_table('cars_country', 'general_country')
        if not db.dry_run:
            # For permissions to work properly after migrating
            orm['contenttypes.ContentType'].objects.filter(app_label='cars', model='country').update(app_label='general')

    def backwards(self, orm):
        pass

Delete:

# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

    depends_on = (
        ('general', '0002_create_country'),
    )

    def forwards(self, orm):

        db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['general.Country'], null=True))


    def backwards(self, orm):

        db.rename_table('general_country', 'cars_country')
        db.alter_column('cars_club', 'country_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['cars.Country'], null=True))
Community
  • 1
  • 1
margusholland
  • 3,306
  • 1
  • 23
  • 30
  • According to the South docs: _Note that you can only access models that have been frozen; South automatically includes anything that could be reaches via foreign keys or many-to-many relationships, but if you want to add other models in, simply pass --freeze appname to the ./manage.py datamigration command._ – dgel Nov 28 '12 at 01:51
  • So it sounds like you need to have the ContentTypes app passed to --freeze at some point. Don't completely understand how, but may be a hint in the right direction. – dgel Nov 28 '12 at 01:54
  • Assuming that ContentTypes app won't be target of any schemamigration, how bad is it to import the models from the Django project? _from django.contrib.contenttypes.models import ContentType_ _ContentType.objects.filter(app_label='common', model='cat').update(app_label='specific')_ – JCJS Apr 07 '14 at 09:13

1 Answers1

4

Ok, found the solution. The freezing notice from dgel got me checking the South documentation and there’s a notice about the ORM migration: This is accomplished by serialising the models into a large dictionary called models at the bottom of every migration. It’s easy to see; it’s the large chunk of dense code at the bottom.

So basically I just needed to move the orm['contenttypes.contenttype] to the second migration file, as the contenttype models dictionary was already there. And now everything seems to work as it should.

margusholland
  • 3,306
  • 1
  • 23
  • 30
  • 2
    Or simply create your datamigration as: `./manage.py datamigration app_name --auto --freeze contenttypes` – geoom May 23 '15 at 05:16