Friday, December 30, 2011

Misfiring signal handler

Hey guys,

I have an odd problem where a post_save signal handler is getting
called from a class different than what I specified as the sender.
The code is at and also below for when
the pastebin is deleted.

I'm connecting the rating_denormalizer() function to the post_save
signal of the Rating model. However, when I visit the site with a
fresh browser a new Session is created and for some reason, this
signal handler is called with a Session instance.

I was able to fix this by moving this code from users/ to
bars/, directly importing Rating instead of using get_model,
and moving the AppUser import inside rating_denormalizer(). However,
I still would like to figure out why this was happening.

# users/
from django.db.models.signals import post_save
from django.db.models import F, get_model

Rating = get_model('bars','Rating')
AppUser = get_model('users','AppUser')

def rating_denormalizer(sender, instance, created, **kwargs):
Post-save signal handler on Rating to keep the denormalized fields
AppUser up-to-date
if created:
rating_count=F('rating_count') + 1,
post_save.connect(rating_denormalizer, sender=Rating,

# Get a 500 error when going to /admin/ with a fresh browser
2011-12-27 16:45:07,608 ERROR Internal Server Error: /admin/
Traceback (most recent call last):
File "/var/www/python/myapp/lib/python2.6/site-packages/django/core/
handlers/", line 178, in get_response
response = middleware_method(request, response)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/
contrib/sessions/", line 36, in process_response
File "/var/www/python/myapp/lib/python2.6/site-packages/django/
contrib/sessions/backends/", line 63, in save, using=using)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/db/
models/", line 460, in save
self.save_base(using=using, force_insert=force_insert,
File "/var/www/python/myapp/lib/python2.6/site-packages/django/db/
models/", line 570, in save_base
created=(not record_exists), raw=raw, using=using)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/
dispatch/", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/var/www/python/myapp/App/users/", line 13, in
AttributeError: 'Session' object has no attribute 'user_id'

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment