Search code examples
djangopython-2.7django-signalsdjango-1.5

django signals module object has no attribute connect


I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog.

This is the 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)

When I try to run the server, it gives me an error:

AttributeError: 'module' object has no attribute 'connect'

Please help me out. Would be much appreciate. Thank you.


Solution

  • replace

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

    dispatcher.connect -> Signal.connect

    dispatcher is module, interpreter tell it for you.