python任意代码都可以缩进去_可以迁移.RunPython运行任意python代码?

我希望在迁移结束时包括对许多原始SQL文件(函数、触发器等)的处理。在

由于RunPython()使用带有两个实例(一个应用程序和一个SchemaEditor)的callable,我严重怀疑(我稍微回顾了一下源代码)我是否可以用纯python代码调用函数,它似乎只能执行ORM操作。我是否应该在RunPython中使用execute_from_command_line()?或者这条路注定要失败?还是做得不好?在from __future__ import unicode_literals

from django.db import migrations

def load_sql(apps, schema_editor):

from os.path import normpath, dirname, isfile, join

from os import listdir

sql_folder_path = '/backoffice/sql/'

def load_raw_sql(folder_inside):

folder_path = join(sql_folder_path, folder_inside)

sql_files = [join(folder_path, f) for f in listdir(folder_path) if isfile(join(folder_path, f))]

for sql_file in sql_files:

with open(sql_file, 'r') as g:

migrations.RunSQL(g.read())

folders = ['functions', 'index', 'triggers']

for folder in folders:

load_raw_sql(folder)

class Migration(migrations.Migration):

dependencies = [

('app1', '0001_squashed_0018_auto_20150616_0708'),

]

operations = [

migrations.RunPython(load_sql),

]

我们使用的是PostGreSQL。

提前感谢您的回答。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值