2014-02-20 62 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