Django内置信号问题:使用post_保存时出错
我正在构建一个应用程序,当新的应用程序出现时通知用户。为此,我使用了Django内置信号问题:使用post_保存时出错,django,content-type,django-signals,threaded-comments,Django,Content Type,Django Signals,Threaded Comments,我正在构建一个应用程序,当新的应用程序出现时通知用户。为此,我使用了post\u save信号。这是我的型号.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 dateti
post\u save
信号。这是我的型号.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\u get\u pk\u val()必须以ThreadedComment实例作为第一个参数调用(而不是获取任何内容)
。回溯和调试器说,当创建BuzzEvent对象调用signals.pre_init.send
时会发生这种情况。我现在无法破解django代码,有什么明显的解决方案或想法吗?看起来它应该是模型实例(它是注释
),而不是模型类(它是发送者
),作为内容对象参数:
BuzzEvent.objects.create(content_object=comment,
user=user)
看起来它应该是模型实例(它是
comment
is),而不是模型类(它是sender
is),作为content\u对象参数:
BuzzEvent.objects.create(content_object=comment,
user=user)