我一直在爲這個問題奮鬥了5個小時,我感覺這是一個簡單的解決方案,我只是俯瞰。Django錯誤'信號'對象沒有'保存'屬性
我試圖綁定第三方模塊(Django Activity Stream),它使用一系列發送者和接收者將關於用戶活動的數據發佈到數據庫表。一切都設置正確,但我嘗試運行時遇到了一個'Signal' Object has No Attribute 'Save'
錯誤。
我懷疑問題出在我的語法某處。我剛剛開始使用Signals,所以我可能會忽略一些經驗豐富的老手。
在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的活動流應用程序有此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")
感謝您的回答。我正在考慮自己寫這個應用程序,但後來發現我認爲是一個很好的捷徑。無論如何,看起來我最終會自行寫作。我想這就是我試圖偷工減料的結果!再次感謝您的明確迴應。 – 2010-07-17 13:12:41