views:

32

answers:

1

Hello!

I'm building an app that notifies user when new ThreadedComments appear. For this I'm using post_save signal. Here's my models.py:

from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from datetime import datetime

from threadedcomments.models import ThreadedComment
from django.db.models.signals import post_save

from blog.models import Post
from topics.models import Topic

class BuzzEvent(models.Model):
    user = models.ForeignKey(User)
    time = models.DateTimeField(default=datetime.now)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()

    def __unicode__(self):
        return self.content_object

def buzz_on_comment(sender, **kwargs):
    # This is called when there is a new ThreadedComment
    comment = kwargs['instance']

    user_attr_names = {
                  'post'    : 'author',
                  'topic'   : 'creator',
                  'tribe'   : 'creator',
                 }

    user = getattr(comment.content_object, 
                   user_attr_names[comment.content_type.model])

    BuzzEvent.objects.create(content_object=sender,
                             user=user,
                             time=datetime.now())

post_save.connect(buzz_on_comment, sender=ThreadedComment)

The problem is when a new ThreadedComment is created, I get an error: unbound method _get_pk_val() must be called with ThreadedComment instance as first argument (got nothing instead). Traceback and debugger says it happens when creating BuzzEvent object calls signals.pre_init.send. I'm unable to hack django code right now, is there any obvious solution or idea?

+1  A: 

Looks like it should be model instance (which comment is) instead of model class (which sender is) as the content_object argument:

BuzzEvent.objects.create(content_object=comment,
                         user=user)
d.m
thanks a lot, d.m!
martinthenext