django - signals not working

后端 未结 3 2060
谎友^
谎友^ 2020-12-08 00:14

I am trying to create activity streams of users from their status.

models:

class Status(models.Model):
    body = models.TextField(max_length=200)
           


        
3条回答
  •  天涯浪人
    2020-12-08 00:55

    Seems like your post_save.connect is not executed. You should import signals somewhere. For django 1.7 it is recommended to do this in the app's config ready() function. Read the "Where should this code live?" side note in the docs.

    For example if your app is called activity:

    activity/__init__.py

    default_app_config = 'activity.apps.ActivityAppConfig'
    

    activity/apps.py

    from django.apps import AppConfig
    
    class ActivityAppConfig(AppConfig):
        name = 'activity'
    
        def ready(self):
            import activity.signals
    

    And don't forget to add dispatch_uid to your connect() call:

    post_save.connect(create_activity_item, sender=Status,
                      dispatch_uid="create_activity_item")
    

    UPDATE: name attribute of ContentType is always in lower case. So you should change the if statement to:

    if ctype.name == 'status':
    

提交回复
热议问题