Signal : send a parameter to a receiver function

224 views
Skip to first unread message

alecs

unread,
Apr 14, 2010, 3:32:29 AM4/14/10
to Django users
How can I send a parameter to a receiver function ? This is wrong :
signals.post_save.connect(receiver=refresh_index_page(fragment_name="categories_index",
sender=Post)

I have avoided this problem by wrapping my function in another
function, but a bit inconvenient :)

def refresh_index_page(sender, *args, **kwargs):
invalidate_template_cache(fragment_name="categories_index", *args,
**kwargs)

signals.post_save.connect(receiver=refresh_index_page, sender=Post)

bruno desthuilliers

unread,
Apr 14, 2010, 5:12:49 AM4/14/10
to Django users

If you run python >= 2.5, this should work :

from functools import partial
signals.post_save.connect(
receiver=partial(refresh_index_page,
fragment_name="categories_index"),
sender=Post
)

cf http://docs.python.org/release/2.5/whatsnew/pep-309.html for more
on partial application.

If you need backward compat with older Python versions, there is/was
something pretty similar in Django itself named curry, so this should
works as well:

from django.utils.functional import curry
signals.post_save.connect(
receiver=curry(refresh_index_page,
fragment_name="categories_index"),
sender=Post
)

HTH

alecs

unread,
Apr 14, 2010, 5:33:20 AM4/14/10
to Django users
Thanks :)

mo.mughrabi

unread,
Apr 10, 2014, 5:17:58 PM4/10/14
to django...@googlegroups.com
can someone advise why the below example doesn't work on django 1.6? 
Reply all
Reply to author
Forward
0 new messages