I'm working in django project开发者_Python百科. I have 1 postgresql sql file that need to run only one time after db created. Built-in django signal not quite suit with my case. So I try to write custom django signal but I'm not sure how to start with this case. Does anyone has a good guide. ? :)
The Django docs on signals have improved dramatically, so take a look there if you haven't already. The process is pretty simple.
First create your signal (providing_args
lets you specify the arguments that will get passed when you send your signal later):
import django.dispatch
my_signal = django.dispatch.Signal(providing_args=["first_arg", "second_arg"])
Second, create a receiver function:
from django.dispatch import receiver
@receiver(my_signal)
def my_callback(sender, first_arg, second_arg, **kwargs):
# do something
Finally, send your signal wherever you like in your code (self
as sender is only applicable within your model class. Otherwise, just pass a the model class name):
my_signal.send(sender=self, first_arg='foo', second_arg='bar')
精彩评论