Django内置信号问题:使用post_save时出错
我正在构建一个应用程序,当新的 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 时,我收到错误:必须使用 ThreadedComment 实例作为第一个参数来调用未绑定方法 _get_pk_val() (什么也没有) 。回溯和调试器表示,在创建 BuzzEvent 对象调用 signals.pre_init.send
时会发生这种情况。我现在无法破解 django 代码,有什么明显的解决方案或想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来应该是模型实例(
comment
是)而不是模型类(sender
是)作为 content_object 参数:Looks like it should be model instance (which
comment
is) instead of model class (whichsender
is) as the content_object argument: