正如标题所说,我正在尝试从命令行触发Django信号,而不是在视图/模型中触发,但我无法确切地弄清楚如何做到这一点,或者如何从命令行访问许多Django的工作。我认为您需要在“manage.py shell”中或至少在Python shell中,但我找不到任何文档来帮助我进一步了解。
是否有人知道是否可能首先进行此操作,如果可以的话,如何进行。
谢谢
# file <app folder>/management/commands/closepoll.py
from django.core.management.base import BaseCommand, CommandError
from example.polls.models import Poll
class Command(BaseCommand):
args = '<poll_id poll_id ...>'
help = 'Closes the specified poll for voting'
def handle(self, *args, **options):
for poll_id in args:
try:
poll = Poll.objects.get(pk=int(poll_id))
except Poll.DoesNotExist:
raise CommandError('Poll "%s" does not exist' % poll_id)
poll.opened = False
poll.save()
self.stdout.write('Successfully closed poll "%s"\n' % poll_id)
python manage.py closepoll <poll_id>
进行调用。Command.handle
方法中发送一个信号即可:mysignal = django.dispatch.Signal(providing_args=["foo", "bar"])
...
class Command(BaseCommand):
args = '<foo> <bar>'
def handle(self, foo, bar):
mysignal.send(sender=self, foo=foo, bar=bar)