如何告诉django 1.7将迁移放入特定的文件夹 [英] How can I tell django 1.7 to put migration into specific folder

查看:450
本文介绍了如何告诉django 1.7将迁移放入特定的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遇到一个非常有趣的情况。

I'm running into quite interesting situation.

我需要使用某些字段扩展默认的django的组模型。
我试图首先使用继承,例如继承组模型并修改一些引用,但是似乎我不能更改所有需要的引用,所以这样完全打破了django权限系统。

I need to extend default django's Group model with some fields. I tried to use inheritance first, e.g. inherit from Group model and change some references, but seems I can't change all needed references, so, this way completely breaks django permission system.

然后我发现这个答案:如何扩展Django Group模型?哪里建议使用 field.contribute_to_class()方法。

Then I found this answer: How do I extend the Django Group model? where guy suggested to use field.contribute_to_class() method.

我把这个调整权放在了< ; myapp>。 (不要问我为什么需要团队的角色,这不是我的想法,我只需要他们:D)

I have put this adjustment right above the model definition in < myapp >. (don't ask me why do I need roles for group, it's not my idea, I just need them :D)

if not hasattr(Group, 'roles'):
    field = models.ManyToManyField(
        Role, verbose_name=_('roles'), blank=True,
        help_text=_('List of roles attached to this group'),
           related_name='groups')
    field.contribute_to_class(Group, 'roles')


class MyGroup(Group):

    class Meta:
        proxy = True

    def _sync_permissions(self):
        """
            This method will sync group permissions with all attached Roles.
        """
        self.permissions.clear()
        for role in self.roles.all():
            self.permissions.add(role.permissions)
        self.save()

这部分似乎在工作(它真的修改django.contrib.auth.models.Group模型)

This part seems to be working (it really modifies django.contrib.auth.models.Group model)

但是接下来我需要的是以生成组模型的迁移。
如果我只是运行 ./ manage.py makemigrations< myapp> 它会为Group模型生成迁移,但尝试将其放在django.contrib.auth应用,这绝对不是我需要的。

But what I need next is to generate a migration for the Group model. If I simply run ./manage.py makemigrations <myapp> it generates a migration for Group model, but tries to put it inside django.contrib.auth application, that is definitely not what I need.

所以,我的问题是:

有没有告诉django为组模型生成迁移的方法,但不要在python libs目录下创建迁移文件,而是在< myapp>或只是输出迁移代码?

Is there a way to tell django to generate a migration for Group model, but not to create a migration file under python libs directory, but rather create it inside < myapp > or just output the migration code?

推荐答案

使用 MIGRATION_MODULES ,无论如何,这意味着所有迁移(不仅仅是新的)必须在那里。
您需要复制原始迁移,并在升级Django时手动更新它们

the location where django looks for migrations cn be custominzed using MIGRATION_MODULES in your settings.py, anyway this means that ALL the migrations (not only the new) must be there. You need to copy the original migrations and manually update them when you upgrade Django

您可以创建一个专用软件包,以免与您的迁移冲突$ ​​b $ b Es。

You can create a dedicated package so to not clash with your migrations Es.

MIGRATION_MODULES = {
     'django.contrib.auth' : 'myapp.auth_migrations',
     'myapp': 'myapp.migrations'  # this line is only to clarify. IT'S NOT NEEDED AT ALL 
}

这篇关于如何告诉django 1.7将迁移放入特定的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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