Browse Source

Django users do not have to type in DJANGO_SETTINGS_MODULE when using the celery command

Ask Solem 11 years ago
parent
commit
3a07d82091
3 changed files with 32 additions and 4 deletions
  1. 27 2
      docs/django/first-steps-with-django.rst
  2. 3 0
      docs/whatsnew-3.1.rst
  3. 2 2
      examples/django/proj/celery.py

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

@@ -35,9 +35,15 @@ that defines the Celery instance:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
+    from __future__ import absolute_import
+
+    import os
+
     from celery import Celery
     from celery import Celery
     from django.conf import settings
     from django.conf import settings
 
 
+    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
+
     app = Celery('proj.celery')
     app = Celery('proj.celery')
     app.config_from_object('django.conf:settings')
     app.config_from_object('django.conf:settings')
     app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
     app.autodiscover_tasks(settings.INSTALLED_APPS, related_name='tasks')
@@ -47,13 +53,32 @@ that defines the Celery instance:
         print('Request: {0!r}'.format(self.request))
         print('Request: {0!r}'.format(self.request))
 
 
 Let's explain what happens here.
 Let's explain what happens here.
-First we create the Celery app instance:
+First we import absolute imports from the future, so that our
+``celery.py`` module will not crash with the library:
+
+.. code-block:: python
+
+    from __future__ import absolute_import
+
+Then we set the default :envvar:`DJANGO_SETTINGS_MODULE` 
+for the :program:`celery` command-line program:
+
+.. code-block:: python
+
+    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
+
+You don't need this line, but it saves you from always passing in the
+settings module to the celery program.  It must always come before
+creating the app instances, which is what we do next:
 
 
 .. code-block:: python
 .. code-block:: python
 
 
     app = Celery('proj')
     app = Celery('proj')
 
 
-Then we add the Django settings module as a configuration source
+This is our instance of the library, you can have many instances
+but there's probably no reason for that when using Django.
+
+We also add the Django settings module as a configuration source
 for Celery.  This means that you don't have to use multiple
 for Celery.  This means that you don't have to use multiple
 configuration files, and instead configure Celery directly
 configuration files, and instead configure Celery directly
 from the Django settings.
 from the Django settings.

+ 3 - 0
docs/whatsnew-3.1.rst

@@ -330,6 +330,9 @@ but if you would like to experiment with it you should know that:
 
 
         DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
         DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info
 
 
+    You can also set a default value in your app module so you
+    don't have to type it every time: just see the example in the :ref:`Django
+    guide <django-first-steps>`.
 
 
 To get started with the new API you should first read the :ref:`first-steps`
 To get started with the new API you should first read the :ref:`first-steps`
 tutorial, and then you should read the Django specific instructions in
 tutorial, and then you should read the Django specific instructions in

+ 2 - 2
examples/django/proj/celery.py

@@ -4,11 +4,11 @@ import os
 
 
 from celery import Celery
 from celery import Celery
 
 
+from django.conf import settings
+
 # set the default Django settings module for the 'celery' program.
 # set the default Django settings module for the 'celery' program.
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
 
 
-from django.conf import settings
-
 app = Celery('proj.celery')
 app = Celery('proj.celery')
 
 
 # Using a string here means the worker will not have to
 # Using a string here means the worker will not have to