2014-02-20 25 views
3

我们南方使用我们的schemamigrations和datamigrations。现在我需要在Django中启用缓存,这非常简单。这迫使我在我的终端中使用manage.py createcachetable cache_table。虽然我想用South来自动化这个过程。有没有办法使用South创建缓存表?使用South创建Django Cache表?

回答

3

创建一个新的南datamigration(只是一个空白的迁移):
python manage.py datamigration <app> create_cache_table

编辑生成的迁移。我简单地叫我的缓存表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') 

    ... 

如果您正在使用多个数据库并需要定义使用哪个数据库。请注意0​​而不是db的第二条进口声明。您还需要设置路由指令: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') 

    ... 
+0

感谢样子正是我想要:) – Depado