检测是否在migrate/makemigrations命令的上下文中运行代码

Dan*_*anH 5 python django django-migrations django-1.8

我有一个具有动态选择的模型,如果我能保证在发出django-admin.py migrate / makemigrations命令时运行代码以防止它创建或警告无用的选择更改,我想返回一个空的选择列表.

码:

from artist.models import Performance
from location.models import Location

def lazy_discover_foreign_id_choices():
    choices = []

    performances = Performance.objects.all()
    choices += {performance.id: str(performance) for performance in performances}.items()

    locations = Location.objects.all()
    choices += {location.id: str(location) for location in locations}.items()

    return choices
lazy_discover_foreign_id_choices = lazy(lazy_discover_foreign_id_choices, list)


class DiscoverEntry(Model):
    foreign_id = models.PositiveIntegerField('Foreign Reference', choices=lazy_discover_foreign_id_choices(), )
Run Code Online (Sandbox Code Playgroud)

所以我想如果我能检测到运行上下文,lazy_discover_foreign_id_choices那么我可以选择输出一个空的选择列表.我正在考虑测试sys.argv,__main__.__name__但我希望有可能更可靠的方式或API?

arc*_*lix 9

这是一个相当非hacky的方法(因为django已经为我们创建了标志):

import sys
def lazy_discover_foreign_id_choices():
    if ('makemigrations' in sys.argv or 'migrate' in sys.argv):
        return []
    # Leave the rest as is.
Run Code Online (Sandbox Code Playgroud)

这适用于所有情况.


aum*_*umo 5

我能想到的一个解决方案是makemigrations在实际执行实际操作之前对 Django命令进行子类化以设置标志。

例子:

将该代码放入 中<someapp>/management/commands/makemigrations.py,它将覆盖 Django 的默认makemigrations命令。

from django.core.management.commands import makemigrations
from django.db import migrations


class Command(makemigrations.Command):
    def handle(self, *args, **kwargs):
        # Set the flag.
        migrations.MIGRATION_OPERATION_IN_PROGRESS = True

        # Execute the normal behaviour.
        super(Command, self).handle(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

migrate命令执行相同的操作。

并修改您的动态选择功能:

from django.db import migrations


def lazy_discover_foreign_id_choices():
    if getattr(migrations, 'MIGRATION_OPERATION_IN_PROGRESS', False):
        return []
    # Leave the rest as is.
Run Code Online (Sandbox Code Playgroud)

这是非常hacky但相当容易设置。