我正在构建一个应用程序,当出现新的ThreadedComments时通知用户。为此,我正在使用post_save
信号。这是我的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)
问题是当创建一个新的 ThreadedComment 时,我得到一个错误:unbound method _get_pk_val() must be called with ThreadedComment instance as first argument (got nothing instead)
。Traceback 和调试器说它发生在创建 BuzzEvent 对象调用时signals.pre_init.send
。我现在无法破解 django 代码,有什么明显的解决方案或想法吗?