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.

1 Answer 1

3

replace

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

dispatcher.connect -> Signal.connect

dispatcher is module, interpreter tell it for you.

Sign up to request clarification or add additional context in comments.

3 Comments

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)
try signals.post_save.connect(create_stream_item, sender=modelname). Your variant looks like old django signal connection.
Django has very good and urgent docs - docs.djangoproject.com/en/dev/topics/signals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.