我们使用 South 进行架构迁移和数据迁移。现在我需要在 Django 中启用缓存,这很容易做到。这迫使我在终端中使用 manage.py createcachetable cache_table
。虽然我想用 South 来自动化这个过程。有没有办法可以使用 South 创建缓存表?
最佳答案
创建一个新的南数据迁移(只是一个空白迁移):
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')
...
如果您正在使用多个数据库并且需要定义使用哪个。注意 dbs
的第二个导入语句而不是 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')
...
关于python - 使用 South 创建 Django 缓存表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21910717/