问题描述
我正在尝试设置一个信号,以便在保存有效的表单时,运行一个函数来执行相关任务。
我的应用程序结构是如下;
- 事件
- 帮助者
- __init__.py
- 状态。 py
- 模型
- 信号
- __init__.py
- event.py
- __init__.py
- event.py
- status.py
- 视图
- __init__.py
- event.py
$ b $我相信信号需要在模型之前尽可能早地导入,所以在模型/ __ init __。py
的顶部我有 from .signals import *
。
#views / event.py
class AddEventView(CreateView):
添加事件的视图
model = Event
form_class = EventForm
success_url = reverse_lazy ('events:all_events')
def form_valid(self,form):
self.object = form.save()
signals.event_status.send(
sender = None,request = self.request,event = self.object,status =无
)#发件人应该是self.object吗?
返回超级(AddEventView,self).form_valid(form)
#signals / event.py
from django.dispatch import Signal
event_status =信号(provide_args = [request,event,status])
#helpers / status.py
from ..models import Status,StatusHistory
from。 .models.signals import event_status
def create_status(sender,** kwargs):
为给定的事件创建一个状态
event = kwargs ['event']
status = kwargs ['status']
creator = User.objects.get(pk = event.creator)
尝试:
current_status = StatusHistory.objects.filter(
event = event).order_by('timestamp')[0]
除了IndexError:
#不知道我们是还在这里
pass
如果不是状态:
status = Status.objects.get(description = _(Submitted))
statushistory = StatusHistory.create(
event = event,
event_status = status,
user = creator
)
statushistory.save()
event_status.connect(create_status)
我在Pycharm中运行调试服务器,在 create_status中有一个断点()
function&
>我想也许你只需要导入你的 helpers / status.py
,例如在 models / __ init __。py
p>
否则你的 event_status
信号被定义好,但信号处理程序 create_status
永远不会被Django连接
如果您只有一个该信号的处理程序,将它放在与信号定义相同的模块中可能是有意义的
I'm trying to setup a signal so that when a valid form is saved, a function is ran to carry out a related task.
My app structure is as follows;
- events
- helpers
- __init__.py
- status.py
- models
- signals
- __init__.py
- event.py
- __init__.py
- event.py
- status.py
- views
- __init__.py
- event.py
I believe signals need to be imported as early as possible, before models, so at the top of models/__init__.py
I've got from .signals import *
.
# views/event.py
class AddEventView(CreateView):
"""
View for adding an Event.
"""
model = Event
form_class = EventForm
success_url = reverse_lazy('events:all_events')
def form_valid(self, form):
self.object = form.save()
signals.event_status.send(
sender=None, request=self.request, event=self.object, status=None
) # Should the sender be self.object?
return super(AddEventView, self).form_valid(form)
# signals/event.py
from django.dispatch import Signal
event_status = Signal(providing_args=["request", "event", "status"])
# helpers/status.py
from ..models import Status, StatusHistory
from ..models.signals import event_status
def create_status(sender, **kwargs):
"""
Create a status for a given event.
"""
event = kwargs['event']
status = kwargs['status']
creator = User.objects.get(pk=event.creator)
try:
current_status = StatusHistory.objects.filter(
event=event).order_by('timestamp')[0]
except IndexError:
# Not sure what we're doing here yet.
pass
if not status:
status = Status.objects.get(description=_("Submitted"))
statushistory = StatusHistory.create(
event=event,
event_status=status,
user=creator
)
statushistory.save()
event_status.connect(create_status)
I'm running the debug server in Pycharm with a break point in the create_status()
function & it's never getting hit.
Have I implemented this wrong?
I think maybe you just need to import your helpers/status.py
eg in models/__init__.py
otherwise your event_status
signal gets defined ok but the signal handler create_status
never gets connected by Django
if you only have one handler for that signal it might make sense to put it in the same module as the signal definition
这篇关于Django信号无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!