Login

cron/console bootstrap django

Author:
nstrite
Posted:
August 19, 2007
Language:
Python
Version:
.96
Score:
6 (after 8 ratings)

Call this function as the first thing in your cron, or console script; it will bootstrap Django, and allow you to access all of the Django modules from the console, with out using 'python manage.py shell'

Examples:

    # A script within your django project.
    from django_bootstrap import bootstrap
    bootstrap(__file__)

--- or ---

    # A cron script located outside of your django project
    from django_bootstrap import bootstrap
    bootstrap("/path/to/your/project/root/")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import sys

def bootstrap(path):
    """
    Call this function as the first thing in your cron, or console script; it will bootstrap Django, and allow you to access all of the Django modules from the console, without using 'python manage.py shell'

    Examples:
 
        # A script within your django project.
        from django_bootstrap import bootstrap
        bootstrap(__file__) 
    
    --- or ---
    
        # A cron script located outside of your django project
        from django_bootstrap import bootstrap
        bootstrap("/path/to/your/project/root/")
    """
    path = find_settings_path(path)
    parent_path = os.path.abspath(os.path.join(path, '..'))
    
    # Include path to settings.py directory
    os.sys.path.append(path)
    # Include path to django project directory
    os.sys.path.append(parent_path)
    
    from django.core import management
    try:
        import settings # Assumed to be in the os path
    except ImportError:
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
        sys.exit(1)
        
    management.setup_environ(settings)

def find_settings_path(path):
    """
    Retrieve the path of the settings.py module for a django project. This can be passed into django_utils.bootstrap). You can pass in the  __file__ variable, or an absolute path, and it will find the closest settings.py module.
    """
    if os.path.isfile(path):
        path = os.path.dirname(path)
    
    path = os.path.abspath(path)
    parent_path = os.path.abspath(os.path.join(path, '..'))
    settings_path = os.path.join(path, 'settings.py')

    if os.path.exists(settings_path):
        return path
    elif path != parent_path:
        return find_settings_path(parent_path)
    else:
        raise Exception('Could not find settings path.')

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

buriy (on August 19, 2007):

if path is directory,

os.path.dirname(path)

is synonym for

os.path.abspath(os.path.join(path, '..'))

#

Magus (on August 20, 2007):

I would personally remove line 24, as you're generally not supposed to have the project dir itself on pythonpath, and it could screw up imports.

#

nstrite (on August 21, 2007):

line 24 allows line 30 to work. unless we know the name of your project ahead of time, you need to have the settings.py file in your path (import settings) otherwise you would need from xyz import settings.

#

guettli (on September 6, 2007):

Thank you for this snippet. That was what I was looking for:

from django.core import management management.setup_environ(settings)

#

Please login first before commenting.