How can I call a custom Django manage.py command directly from a test driver?

后端 未结 5 1847
悲&欢浪女
悲&欢浪女 2020-11-29 17:30

I want to write a unit test for a Django manage.py command that does a backend operation on a database table. How would I invoke the management command directly from code?

5条回答
  •  隐瞒了意图╮
    2020-11-29 18:09

    the following code:

    from django.core.management import call_command
    call_command('collectstatic', verbosity=3, interactive=False)
    call_command('migrate', 'myapp', verbosity=3, interactive=False)
    

    ...is equal to the following commands typed in terminal:

    $ ./manage.py collectstatic --noinput -v 3
    $ ./manage.py migrate myapp --noinput -v 3
    

    See running management commands from django docs.

提交回复
热议问题