1
我正在構建一個應用程序,當新的ThreadedComments出現時通知用戶。爲此,我使用post_save
信號。這裏是我的models.py
:Django內置信號問題:使用post_save時出錯
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代碼,是否有任何明顯的解決方案或想法?
非常感謝,D.M! – martinthenext 2010-07-13 19:00:57