6

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:

__init__.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(user=instance.user, 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. And I am not getting any error too. 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.

Aamu
  • 3,431
  • 7
  • 39
  • 61

2 Answers2

11

You have to make sure that the signals are loaded soon after django is started. The one possible way to ensure it is to import the module into __init__.py

# __init__.py
# add the below line and run the project again
import signals
Surya
  • 4,824
  • 6
  • 38
  • 63
  • Hello. I have added `__init__.py` in the signals.py. But even now it doesnt work. Is there anyway to check the errors? – Aamu Nov 29 '13 at 11:55
  • By the way, do I have to place `import signals` to `__init__.py` file. Or place the `__init__.py` in the `signals.py` file? – Aamu Nov 29 '13 at 12:09
  • @Aamu In the app modules you will have `__init__.py` which are definitely loaded when django is loaded. so, drop `import signals` line in that – Surya Nov 29 '13 at 13:26
  • Ok, now I think that works... But I am getting another error: `AttributeError: 'module' object has no attribute 'connect'` – Aamu Nov 29 '13 at 13:31
  • Ok, I had to change some of the code, and it worked. But `import signals` in init.py file did solved the firing up of the signals. Thank you. – Aamu Nov 29 '13 at 14:17
  • Yes, you have to make sure the code is actually loaded before it will do anything. I usually define any signal handlers I want in the same file as the necessary model definitions. – Peter DeGlopper Nov 29 '13 at 18:23
0

Unless you've omitted code, the new si item your signal handler creates is missing the required field user. You probably need to add that to your get_or_create call.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • Hi, thank you for the answer. And yes, I didn't added the user. But now, even after I have added, it does not create a new stream item. – Aamu Nov 29 '13 at 11:29