Skip to content

Commit 976677b

Browse files
update documentation on signal creation for Django-1.7
1 parent 4a1b04c commit 976677b

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/usage.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,25 @@ For example::
2525
"you have received an invitation"
2626
)
2727

28+
Before Django-1.7, the typical way to automatically do this notice type creation
29+
was in a `management.py` file for your app, attached to the syncdb signal.
2830

29-
One good way to automatically do this notice type creation is in a
30-
`management.py` file for your app, attached to the syncdb signal.
31+
Django-1.7 deprecated the `post_syncdb` signal, so this system needs to be changed. One possible way to do it is using a custom `AppConfig`.
3132

3233
Here is an example:
3334

35+
# myapp/signals/handlers.py
3436
from django.conf import settings
35-
from django.db.models import signals
3637
from django.utils.translation import ugettext_noop as _
3738

38-
if "notification" in settings.INSTALLED_APPS:
39-
from pinax.notifications.models import NoticeType
40-
41-
def create_notice_types(app, created_models, verbosity, **kwargs):
39+
def create_notice_types(sender, **kwargs):
40+
if "pinax.notifications" in settings.INSTALLED_APPS:
41+
from pinax.notifications.models import NoticeType
42+
print "Creating notices for myapp"
4243
NoticeType.create("friends_invite", _("Invitation Received"), _("you have received an invitation"))
4344
NoticeType.create("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))
44-
45-
signals.post_syncdb.connect(create_notice_types, sender=NoticeType)
46-
else:
47-
print "Skipping creation of NoticeTypes as notification app not found"
45+
else:
46+
print "Skipping creation of NoticeTypes as notification app not found"
4847

4948
Notice that the code is wrapped in a conditional clause so if
5049
`pinax-notifications` is not installed, your app will proceed anyway.
@@ -53,6 +52,23 @@ Note that the display and description arguments are marked for translation by
5352
using ugettext_noop. That will enable you to use Django's makemessages
5453
management command and use `pinax-notifications` i18n capabilities.
5554

55+
# myapp/apps.py
56+
from django.apps import AppConfig
57+
from django.db.models.signals import post_migrate
58+
59+
from myapp.signals import handlers
60+
61+
class MyAppConfig(AppConfig):
62+
name = 'myapp'
63+
verbose_name = 'My App'
64+
65+
def ready(self):
66+
post_migrate.connect(handlers.create_notice_types, sender=self)
67+
68+
This will call the handler to create notices after the application is migrated.
69+
70+
# myapp/__init__.py
71+
default_app_config = 'myapp.apps.MyAppConfig'
5672

5773
## Notification Templates
5874

0 commit comments

Comments
 (0)