Django 错误“信号”;对象没有属性“保存”;

发布于 2024-09-10 17:34:45 字数 2256 浏览 3 评论 0原文

我已经在这个问题上苦苦挣扎了 5 个小时,我感觉这是一个简单的解决方案,但我只是忽略了。

我正在尝试绑定第三方模块(Django Activity Stream),使用一系列发送者和接收者将有关用户活动的数据发布到数据库表中。一切都已正确设置和安装,但当我尝试运行它时,出现 'Signal' Object has No Attribute 'Save' 错误。

我怀疑问题出在我的语法中。我刚刚开始使用信号,所以我可能会忽略一些老手会立即发现的东西。

views.py 中,我有:

from django.db.models.signals import pre_save
from actstream import action    ##This is the third-party app
from models import Bird

def my_handler(sender, **kwargs):
 action.save(sender, verb='was saved')
 #return HttpResponse("Working Great")

pre_save.connect(my_handler, sender=Bird)

def animal(request):
 animal = Bird()
 animal.name = "Douglas"
 animal.save()

Django Activity Stream 应用程序有这个 signals.py 文件:

from django.dispatch import Signal

action = Signal(providing_args=['actor','verb','target','description','timestamp'])

然后是这个 models.py 文件:

from datetime import datetime
from operator import or_
from django.db import models
from django.db.models.query import QuerySet
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.timesince import timesince as timesince_
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from actstream import action
...
def action_handler(verb, target=None, **kwargs):
    actor = kwargs.pop('sender')
    kwargs.pop('signal', None)
    action = Action(actor_content_type=ContentType.objects.get_for_model(actor),
                    actor_object_id=actor.pk,
                    verb=unicode(verb),
                    public=bool(kwargs.pop('public', True)),
                    description=kwargs.pop('description', None),
                    timestamp=kwargs.pop('timestamp', datetime.now()))
    if target:
        action.target_object_id=target.pk
        action.target_content_type=ContentType.objects.get_for_model(target)

    action.save()

action.connect(action_handler, dispatch_uid="actstream.models") 

I've been struggling with this problem for 5 hours and I have a feeling it's a simple solution that I'm just overlooking.

I'm trying to tie in a third party module (Django Activity Stream) that uses a series of senders and receivers to post data about user activity to a database table. Everything is set up and installed correctly, but I get a 'Signal' Object has No Attribute 'Save' error when I try to run it.

I suspect the problem is in my syntax somewhere. I'm just getting started with Signals, so am probably overlooking something a veteran will spot immediately.

In views.py I have:

from django.db.models.signals import pre_save
from actstream import action    ##This is the third-party app
from models import Bird

def my_handler(sender, **kwargs):
 action.save(sender, verb='was saved')
 #return HttpResponse("Working Great")

pre_save.connect(my_handler, sender=Bird)

def animal(request):
 animal = Bird()
 animal.name = "Douglas"
 animal.save()

The Django Activity Stream app has this signals.py file:

from django.dispatch import Signal

action = Signal(providing_args=['actor','verb','target','description','timestamp'])

And then this models.py file:

from datetime import datetime
from operator import or_
from django.db import models
from django.db.models.query import QuerySet
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.timesince import timesince as timesince_
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from actstream import action
...
def action_handler(verb, target=None, **kwargs):
    actor = kwargs.pop('sender')
    kwargs.pop('signal', None)
    action = Action(actor_content_type=ContentType.objects.get_for_model(actor),
                    actor_object_id=actor.pk,
                    verb=unicode(verb),
                    public=bool(kwargs.pop('public', True)),
                    description=kwargs.pop('description', None),
                    timestamp=kwargs.pop('timestamp', datetime.now()))
    if target:
        action.target_object_id=target.pk
        action.target_content_type=ContentType.objects.get_for_model(target)

    action.save()

action.connect(action_handler, dispatch_uid="actstream.models") 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

沦落红尘 2024-09-17 17:34:45

您的主要问题在于维护编码风格的纪律,或者更确切地说,在这种情况下,缺乏纪律。您会发现,如果不使用相同的名称来引用同一模块中的多个内容,则更容易识别代码中的问题;为每个对象指定一个唯一且有意义的名称,并仅使用该名称来引用它。

Your main problem is in the discipline in maintaining coding style, or rather in this case, lack of. You will find that it is easier to identify problems in your code if you do not use the same name to refer to multiple things within the same module; give each object a unique, meaningful name, and refer to it using only that name.

披肩女神 2024-09-17 17:34:45

这里的底线是该项目的文档包含错误的代码。这行:

action.save(sender, verb='was saved')

永远不会起作用。 from actstream import 操作 最终从 actstream.signals 导入信号,而信号没有也从来没有 save 方法。尤其是使用如此奇怪的sender, verb签名。

起初我以为作者可能在子类化 Signal 方面做了一些奇怪的事情,但在查看了代码库的其余部分后,情况并非如此。我不完全确定这些文档的意图是什么,但是在处理程序中正确的做法是保存一个新的 Action (从 actstream.models 导入)) 实例,或者对您的模型执行某些操作。

遗憾的是,该项目的存储库有一组非常遗憾的测试/示例,因此如果不亲自下载并尝试该应用程序,我无法告诉您那里需要发生什么。您可以尝试联系作者,或者只是尝试寻找文档更好/维护更好的 Activity Streams 应用程序。

The bottom line here is that the docs for that project contain bad code. This line:

action.save(sender, verb='was saved')

isn't ever going to work. The from actstream import action ultimately imports a signal from actstream.signals, and signals do not and never have had a save method. Especially not with such an odd signature of sender, verb.

At first I thought maybe the author had done something odd with subclassing Signal, but after looking at the rest of the codebase, that's just not the case. I'm not entirely sure what the intention of those docs was supposed to be, but the right thing to do in your handler will either be to save a new Action (imported from actstream.models) instance, or to do something with your model.

Sadly, the project's repository has a pretty sorry set of tests/examples, so without downloading and trying the app myself, I can't tell you what needs to happen there. You might try contacting the author or simply try finding a better-documented/better-maintained Activity Streams app.

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