django信号模块对象没有属性连接

时间:2013-11-29 12:45:35

标签: django python-2.7 django-signals django-1.5

我正在尝试创建一个项目,以便在博客的帮助下创建用户的Feed /活动供稿。

这是signals.py:

from django.db.models import signals
from django.contrib.contenttypes.models import ContentType
from django.dispatch import dispatcher
from blogs.models import Blog
from picture.models import Photo
from models import StreamItem

def create_stream_item(sender, instance, signal, *args, **kwargs):

    # Check to see if the object was just created for the first time

    if 'created' in kwargs:
        if kwargs['created']:
            create = True

            # Get the instance's content type

            ctype = ContentType.object.get_for_model(instance)

            if create:
                si = StreamItem.objects.get_or_create(content_type=ctype, object_id=instance.id, pub_date = instance.pub_date)

 # Send a signal on post_save for each of these models

for modelname in [Blog, Photo]:
    dispatcher.connect(create_stream_item, signal=signals.post_save, sender=modelname)

当我尝试运行服务器时,它会给我一个错误:

  

AttributeError:'module'对象没有属性'connect'

请帮帮我。非常感谢。谢谢。

1 个答案:

答案 0 :(得分:3)

替换

from django.dispatch import dispatcher -> from django.dispatch import Signal

dispatcher.connect -> Signal.connect

调度员是模块,口译员告诉你。