浏览代码

Django tutorial: @shared_task would not work if app not loaded

Ask Solem 11 年之前
父节点
当前提交
a8e33fd2f8
共有 2 个文件被更改,包括 23 次插入2 次删除
  1. 18 2
      docs/django/first-steps-with-django.rst
  2. 5 0
      examples/django/proj/__init__.py

+ 18 - 2
docs/django/first-steps-with-django.rst

@@ -52,8 +52,24 @@ that defines the Celery instance:
     def debug_task(self):
         print('Request: {0!r}'.format(self.request))
 
-Let's explain what happens here.
-First we import absolute imports from the future, so that our
+Then you need to import this app in your :file:`proj/proj/__init__py`
+module.  This ensures that the app is loaded when Django starts
+so that the ``@shared_task`` decorator (mentioned later) will use it:
+
+:file:`proj/proj/__init__.py`:
+
+.. code-block:: python
+
+    from __future__ import absolute_import
+
+    from .celery import app as celery_app
+
+Note that this example project layout is suitable for larger projects,
+for simple projects you may use a single contained module that defines
+both the app and tasks, like in the :ref:`tut-firsteps` tutorial.
+
+Let's break down what happens in the first module,
+first we import absolute imports from the future, so that our
 ``celery.py`` module will not crash with the library:
 
 .. code-block:: python

+ 5 - 0
examples/django/proj/__init__.py

@@ -0,0 +1,5 @@
+from __future__ import absolute_import
+
+# This will make sure the app is always imported when
+# Django starts so that shared_task will use this app.
+from .celery import app as celery_app