[Django]-Creating Django Cache table using South?

3👍

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')

    ...
👤Brian

Leave a comment