163👍
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':
19👍
If you write everything in signals.py
correctly but not working, then check these steps… (suppose in an app named AppName)
-
in
__init__.py
, put the linedefault_app_config = 'AppName.apps.AppnameConfig'
-
in
apps.py
file, put the blockfrom django.apps import AppConfig class AppnameConfig(AppConfig): name = 'AppName' def ready(self): import AppName.signals
- [Django]-Django request to find previous referrer
- [Django]-Celery. Decrease number of processes
- [Django]-How to server HTTP/2 Protocol with django
17👍
let’s say your apps name is blog
, in the settings.py file of your project ensure to register the blog
app in the INSTALLED_APP variable of your main project’s settings.py file as blog.apps.BlogConfig
and not just blog
. That worked for me.
- [Django]-How to recursively query in django efficiently?
- [Django]-Linking to the django admin site
- [Django]-How to check (in template) if user belongs to a group
6👍
The simple, scalable, repeatable, reusable answer is…
If you plan to use signals (ex: signals.py
) within an app (ex: posts
), simply get in the habit of adding this method to your apps.py
AppConfig
class everytime.
def ready(self):
from . import signals
You don’t need to touch __init__.py
like others have said. Your signals will then work.
- [Django]-OperationalError, no such column. Django
- [Django]-Django setUpTestData() vs. setUp()
- [Django]-Where to store secret keys DJANGO
3👍
Without touching apps.py this worked for me.
class MyModel(models.Model):
""" MyModel fields go """
body = models.TextField(max_length=200)
pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
def post_save_actions(sender, instance, created, **kwargs):
if created:
pass
# post save actions if new instance is created,
# do something with `instance` or another models
# be careful about circular imports. \m/
and the signals hook,
post_save.connect(post_save_user_actions, sender=MyModel)
- [Django]-Create a field whose value is a calculation of other fields' values
- [Django]-Set Django's FileField to an existing file
- [Django]-Django: How to set a field to NULL?
0👍
You should add
default_app_config = ‘activity.apps.ActivityAppConfig’
in __init__.py
file
- [Django]-Django : Testing if the page has redirected to the desired url
- [Django]-Django's self.client.login(…) does not work in unit tests
- [Django]-Distributed task queues (Ex. Celery) vs crontab scripts
0👍
First try with these configurations
-
in
__init__.py
filedefault_app_config = 'AppName.apps.AppNameConfig'
-
in
apps.py
filefrom django.apps import AppConfig class AppNameConfig(AppConfig): name = 'AppName' def ready(self): from . import signals # import AppName.signals
But if your application still not working, then don’t write app configurations in __init__.py
file, include your app configrations in settings.py
file
INSTALLED_APPS = [
'AppName.apps.AppNameConfig',
]
- [Django]-Unsupported operand type(s) for *: 'float' and 'Decimal'
- [Django]-AttributeError: 'module' object has no attribute 'tests'
- [Django]-What is "load url from future" in Django