33π
β
You can write anything as a migration. Thatβs the point!
Once you have South
up and running, type in python manage.py schemamigration myapp --empty my_custom_migration
to create a blank migration that you can customize.
Open up the XXXX_my_custom_migration.py
file in myapp/migrations/
and type in your custom SQL migration there in the forwards
method. For example you could use db.execute
The migration might look something like this:
class Migration(SchemaMigration):
def forwards(self, orm):
db.execute("CREATE FULLTEXT INDEX foo ON bar (foobar)")
print "Just created a fulltext index..."
print "And calculated {answer}".format(answer=40+2)
def backwards(self, orm):
raise RuntimeError("Cannot reverse this migration.")
# or what have you
$ python manage.py migrate myapp XXXX # or just python manage.py migrate.
"Just created fulltext index...."
"And calculated 42"
3π
In newer versions of Django, you can create an empty migration for execute custom sql: python3 manage.py makemigrations --empty app_name
Then, in the generated migration:
from django.db import migrations
class Migration(migrations.Migration):
operations = [
migrations.RunSQL(
sql="CREATE FULLTEXT INDEX `index_name` on table_name(`column_name`);",
reverse_sql="ALTER TABLE table_name DROP INDEX index_name"
)
]
π€Fede Bertola
- [Django]-How do I match the question mark character in a Django URL?
- [Django]-Django 1.7 β App 'your_app_name' does not have migrations
- [Django]-Django: show the count of related objects in admin list_display
Source:stackexchange.com