Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Datamigration for external app

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.

like image 298
stett Avatar asked Apr 08 '15 17:04

stett


1 Answers

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.

like image 181
frnhr Avatar answered Sep 24 '22 05:09

frnhr