Django内置信号问题:使用post_save时出错

发布于 2024-09-09 08:30:17 字数 1731 浏览 7 评论 0原文

我正在构建一个应用程序,当新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-09-16 08:30:17

看起来应该是模型实例(comment 是)而不是模型类(sender 是)作为 content_object 参数:

BuzzEvent.objects.create(content_object=comment,
                         user=user)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文