我如何挂载到django的django迁移1.8 [英] How can i hook into django migrations for django 1.8

查看:128
本文介绍了我如何挂载到django的django迁移1.8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.8。

I am using django 1.8.

现在我需要添加一些自定义字段的权限,所以我已经从这样的python模型创建了一个YML文件

Now i need to add some custom fields based permisions so i have created a YML file from python models like this

description:
    permissions: ['ADMIN']
award:
    permissions: ['USER']

它的工作正常但我的问题,如果一些chnages字段名称或重新发现一些字段,那么我也需要反映在我的yml文件中。

Its working fine but my issue if some chnages the field names or reomves some field then i also need to reflect that in my yml file as well.

所以我在想,当用户运行迁移,然后如果有一些我可以看到

SO i was thinking when user runs the migration then if some how i could be able to see


  1. 如果字段被删除,那么我需要从yml文件中删除该条目

  2. 如果字段被重命名,那么我需要重命名

  3. 如果添加了新的字段,那么我需要为其添加空行

我这样做

推荐答案

创建迁移时,您可以手动将基本迁移类更改为自定义子类, code>应用方法

When migration created, you can manually change the base migration class to your custom subclass with overridden apply method

from django.db import migrations

class MyBaseMigration(migrations.Migration):
    def apply(self, project_state, schema_editor, collect_sql=False):
        for operation in self.operations:
            """
            Examine operation classes here and provide end-user notes
            """
         return super(MyBaseMigration, self).apply(project_state, schema_editor, collect_sql=collect_sql)

这篇关于我如何挂载到django的django迁移1.8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆