Question

We use South for our schemamigrations and datamigrations. Now I need to enable the cache in Django which is quite simple to do. That forced me to use manage.py createcachetable cache_table in my terminal. Though I would like to automate this process with South. Is there a way I can create a cache table using South ?

Was it helpful?

Solution

Create a new South datamigration (just a blank migration):
python manage.py datamigration <app> create_cache_table

Edit the generated migration. I called my cache table simply cache.

import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from django.core.management import call_command # Add this import

class Migration(DataMigration):
    def forwards(self, orm):
        call_command('createcachetable', 'cache')

    def backwards(self, orm):
        db.delete_table('cache')

    ...

If you are using multiple databases and need to define which to use. Note the second import statement for dbs instead of db. You also will need to set up routing instructions: https://docs.djangoproject.com/en/dev/topics/cache/#multiple-databases.

import datetime
from south.db import dbs # Import dbs instead of db
from south.v2 import DataMigration
from django.db import models
from django.core.management import call_command # Add this import

class Migration(DataMigration):
    def forwards(self, orm):
        call_command('createcachetable', 'cache', database='other_database')

    def backwards(self, orm):
        dbs['other_database'].delete_table('cache')

    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top