Signals not working as expected

45 views
Skip to first unread message

Aamu Padi

unread,
Nov 28, 2013, 5:45:22 PM11/28/13
to django...@googlegroups.com
I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog.

These are the models -

    class StreamItem(models.Model):
        user = models.ForeignKey(User)
         content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        pub_date = models.DateTimeField(default=datetime.now)
        content_object = generic.GenericForeignKey('content_type', 'object_id')

        @property
        def content_class(self):
            return self.content_type.model


    class Blog(models.Model):
        user = models.ForeignKey(User)
        title = models.CharField(max_length=300)
        body = models.TextField()
        pub_date = models.DateTimeField(default=datetime.now)
   
   
    class Photo(models.Model):
        user = models.ForeignKey(User)
        title = models.CharField(max_length=200)
        image = models.ImageField(upload_to=get_upload_file_name)
         pub_date = models.DateTimeField(default=datetime.now)

And 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 create a blog or upload a photo, the `signal` does not work. But I can manually add items to the `StreamItem` app using the admin, and the StreamItem does work as I want it to be. I think there's problem with the signals.py. Please help me out. Would be much appreciate. Thank you.

Jérôme Thiard

unread,
Nov 29, 2013, 10:58:49 AM11/29/13
to django...@googlegroups.com

You can put signal handling and registration code anywhere you like. However, you’ll need to make sure that the module it’s in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app’s models.py a good place to put registration of signal handlers.

 I think you have to put your connection code in models.py (your `create_stream_item` function can still live in signals.py) in order to be sure connections are made early enough.

2013/11/28 Aamu Padi <aamu...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWtGQPpE7eexnis6EpC2-_WtLcf0ZY-uhvuschj7Tg3MVA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Aamu Padi

unread,
Nov 29, 2013, 11:06:55 AM11/29/13
to django...@googlegroups.com
Thank you for the answer. I am a newbie, would be very grateful if you could please show me how to do that?


Tom Evans

unread,
Nov 29, 2013, 11:14:20 AM11/29/13
to django...@googlegroups.com
On Fri, Nov 29, 2013 at 4:06 PM, Aamu Padi <aamu...@gmail.com> wrote:
> Thank you for the answer. I am a newbie, would be very grateful if you could
> please show me how to do that?

Put the code in signals.py at the bottom of models.py and delete signals.py

Cheers

Tom

Robin Lery

unread,
Nov 29, 2013, 11:25:54 AM11/29/13
to django...@googlegroups.com
Do i put the codes to all the app's -  models.py?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Robin Lery

unread,
Nov 29, 2013, 11:26:09 AM11/29/13
to django...@googlegroups.com
Yes.

Robin Lery

unread,
Nov 29, 2013, 11:45:18 AM11/29/13
to django...@googlegroups.com
Ok, thank you all!
Reply all
Reply to author
Forward
0 new messages