3

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.

Aamu
  • 3,431
  • 7
  • 39
  • 61

1 Answers1

3

replace

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

dispatcher.connect -> Signal.connect

dispatcher is module, interpreter tell it for you.

Nikita
  • 2,780
  • 3
  • 15
  • 12
  • 1
    Thanks for the answer. But after the above changes, am getting another error: `unbound method connect() must be called with Signals instance as a first argument (got function instance instead)` – Aamu Nov 29 '13 at 13:27
  • 2
    try `signals.post_save.connect(create_stream_item, sender=modelname)`. Your variant looks like old django signal connection. – Nikita Nov 29 '13 at 14:03
  • 1
    Django has very good and urgent docs - https://docs.djangoproject.com/en/dev/topics/signals/. – Nikita Nov 29 '13 at 14:23