(Django) Create a management command that will override the default settings in BaseCommand

We always run our tests with settings_test, for example: ./ manage.py test someapp --settings = settings_test. The problem is that sometimes a problem arises in order to add this option.

I would like to introduce a generic application that just has the test.py management team. Depending on the placement in the INSTALLED_APPS setting, it overrides the default value. Inside the command itself, I would like to change the default value of the -settings parameter. How can i do this?

I know that I can create a local.py file similar to manage.py but with settings_test instead of settings. However, the fact is that it is still being implemented. /manage.py, but with the default settings_test parameter instead of typing "--settings = settings_test" after the test. /manage.py someapp

+2
source share
3 answers

You can override the default commands by executing them in your application (say: common_app).

Create a package management.commandsin common_app. And run the test command there:

# this is commonapp/management/commands/test.py
try:
    from south.management.commands import test
except ImportError:
    from django.core.management.commands import test


class Command(test.Command):
    def handle(self, *args, **kwargs):
        kwargs['settings'] = kwargs.get('settings', 'common_app.settings')
        super(Command, self).handle(*args, **kwargs)

settings.py common_app, '--settings ='. "settings_test", settings_test.py, python.

South , . - ... .

__init__.py , "" "" packages.

RTFM Django.

+2

, manage.py , , ( ) . , , , , , , manage.py - , , , - , :

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

    argv = sys.argv
    try:
        if argv[1] == 'test' and not any([k.startswith('--settings') for k in argv]):
            os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.test_settings"
    except IndexError:
        pass

    from django.core.management import execute_from_command_line
    execute_from_command_line(argv)

, , , - .

+2

django, @Chris Wesseling .

., , , INSTALLED_APPS, ..

  • to override the command runserverand you use 'django.contrib.staticfiles'place the application before the staticfiles application
  • to override the command testand you are using a test application, i.e. django_nose, the same thing, place the application in front of the django_nose application.
0
source

All Articles