A management.py loading customized SQL feeding it raw to the database backend.
Just put it as management.py in your app and put whatever SQL you want run after syncdb in app/sql/custom.backend_driver.sql.
If the backend_driver is skipped the SQL will be loaded no matter database backend.
Since it is run after syncdb it will also be run for test.
| 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 | import os, sys
from django.db.models import signals
from django.db import connection, transaction
from django.conf import settings
def load_customized_sql(app, created_models, verbosity=2, **kwargs):
    app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__),      'sql'))
    custom_files = [os.path.join(app_dir, "custom.%s.sql" % settings.           DATABASE_ENGINE),
                    os.path.join(app_dir, "custom.sql")]
    for custom_file in custom_files: 
        if os.path.exists(custom_file):
            print "Loading customized SQL for %s" % app.__name__
            fp = open(custom_file, 'U')
            cursor = connection.cursor()
            try:
                cursor.execute(fp.read().decode(settings.FILE_CHARSET))
            except Exception, e:
                sys.stderr.write("Couldn't execute custom SQL for %s" % app.    __name__)
                import traceback
                traceback.print_exc()
                transaction.rollback_unless_managed()
            else:
                transaction.commit_unless_managed()
        
signals.post_syncdb.connect(load_customized_sql)
 | 
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 3 weeks ago
- get_object_or_none by azwdevops 5 months, 2 weeks ago
- Mask sensitive data from logger by agusmakmun 7 months, 1 week ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
Comments
Thanks, it works for me with some modifications as shown below:
#
Also add management command as below, it needs a logger. See below
#
Please login first before commenting.