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 http://dpaste.com/hold/679348/ 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/signals.py to
bars/signals.py, 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/signals.py
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
on
AppUser up-to-date
"""
if created:
AppUser.objects.filter(pk=instance.user_id).update(
rating_count=F('rating_count') + 1,
last_rating=instance.created,
)
post_save.connect(rating_denormalizer, sender=Rating,
dispatch_uid='rating_denormalizer')


# 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/base.py", line 178, in get_response
response = middleware_method(request, response)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/
contrib/sessions/middleware.py", line 36, in process_response
request.session.save()
File "/var/www/python/myapp/lib/python2.6/site-packages/django/
contrib/sessions/backends/db.py", line 63, in save
obj.save(force_insert=must_create, using=using)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/db/
models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/var/www/python/myapp/lib/python2.6/site-packages/django/db/
models/base.py", 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/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/var/www/python/myapp/App/users/signals.py", line 13, in
rating_denormalizer
AppUser.objects.filter(pk=instance.user_id).update(
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 django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment