7

Is there any way to properly create data-migrations for third party Django apps?

Running python manage.py makemigrations --empty <externa-app-label> works, but creates the migrations in the app's directory (which is inside the virtual environment... don't want to mess with that).


Here's my case:

I need to replace one of the internal apps of a Django project with an external app with similar functionality, and then remove the old internal app. These apps have models, and there is an existing database for the project which will need to be migrated. I would associate the data-migration with the old app if I weren't going to be deleting it later.

A simpler example of the need for something like this might be just needing to fill a third-party app with some initial data.

1 Answer 1

2

Just create the data migration as part of one of your other internal apps, and just do the data manipulation there (maybe even create a temporary app just for that purpose?)

The important bit is to add a new dependency in the data migration file. Something like this, but of course look up the latest migration name in the extenralapp/migrations directory (or some other directory if overridden in settings.MIGRATION_MODULES).

class Migration(migrations.Migration):

    dependencies = [
        ('yourapp', '0004_auto_20151216_1509'),
        ('externalapp', '0011_20010203_1415'),  # this line
    ]

    ...

A related thing...

If using ContentType and / or auth.Permission models, you might run into problems trying to fetch them. Both those models are created at the end of a successful manage.py migrate command.

In this case it might very well happen that your migration will run fine on its own, but fail if you run all the migrations in one go (i.e. on a clean db). For more info and a workaround, see #23422 Cannot add Permission to Group in data migration.

2
  • 4
    I have the same issue and when I do what this answer suggests, the operation tries to happen on the 'yourapp' model rather than the 'externalapp' model and generates an error. For example, if I want to add a field to the shop_productvariation table (i.e. the productvariation model in the shop module where 'shop' would be the 'externalapp' in this example) I get an error that says KeyError: ('yourapp', 'productvariation'). I'm using Django 1.8. I can't seem to get it to operate on the 'externalapp' module unless I put the migration actually into the external app's migration folder.
    – andrewmo
    Nov 3, 2015 at 20:58
  • @andrewmo This answer only works for data migrations. And in any case, you shouldn't try to change models from external apps. Instead, try extending them in your app, and migrate there. But that's another question altogether.
    – frnhr
    Nov 3, 2015 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.