使用South创建Django Cache表?

时间:2014-02-20 14:27:49

标签: python django caching django-south

我们使用South来进行架构和数据迁移。现在我需要在Django中启用缓存,这很简单。这迫使我在终端中使用manage.py createcachetable cache_table。虽然我想与South自动化这个过程。有没有办法可以使用South创建缓存表?

1 个答案:

答案 0 :(得分:3)

创建一个新的南数据迁移(只是空白迁移):
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')

    ...