使用South创建Django Cache表?
作者:互联网
我们将South用于我们的schemamigrations和datamigrations.现在,我需要在Django中启用缓存,这非常简单.这迫使我在终端中使用manage.py createcachetable cache_table.尽管我想与South自动化此过程.有没有一种方法可以使用South创建缓存表?
解决方法:
创建一个新的South数据迁移(只是一个空白迁移):
python manage.py datamigration< app> create_cache_table
编辑生成的迁移.我称我的缓存表为简单缓存.
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')
...
如果您正在使用多个数据库,并且需要定义要使用的数据库.注意第二个import语句是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')
...
标签:caching,django-south,python,django 来源: https://codeday.me/bug/20191122/2056910.html