sqldjangofixturesfixture

How to load sql fixture in Django for User model?


Does anyone knows how to load initial data for auth.User using sql fixtures? For my models, I just got have a < modelname >.sql file in a folder named sql that syncdb does it's job beautifully. But I have no clue how to do it for the auth.User model. I've googled it, but with no success.

Thanks in advance,

Aldo


Solution

  • Thanks for your answers. I've found the solution that works for me, and for coincidence was one of Brian's suggestion. Here it is:

    Firs I disconnected the signal that created the Super User after syncdb, for I have my super user in my auth_user fixture:

    models.py:

    from django.db.models import signals
    from django.contrib.auth.management import create_superuser
    from django.contrib.auth import models as auth_app
    
    
    signals.post_syncdb.disconnect(
        create_superuser,
        sender=auth_app,
        dispatch_uid = "django.contrib.auth.management.create_superuser")
    

    Then I created a signal to be called after syncdb:

    < myproject >/< myapp >/management/__init__.py

    """
    Loads fixtures for files in sql/<modelname>.sql
    """
    from django.db.models import get_models, signals
    from django.conf import settings 
    import <myproject>.<myapp>.models as auth_app
    
    def load_fixtures(app, **kwargs):
        import MySQLdb
        db=MySQLdb.connect(host=settings.DATABASE_HOST or "localhost", \
           user=settings.DATABASE_USER,
        passwd=settings.DATABASE_PASSWORD, port=int(settings.DATABASE_PORT or 3306))
    
        cursor = db.cursor()
    
        try:
            print "Loading fixtures to %s from file %s." % (settings.DATABASE_NAME, \
                settings.FIXTURES_FILE)
            f = open(settings.FIXTURES_FILE, 'r')
            cursor.execute("use %s;" % settings.DATABASE_NAME)
            for line in f:
                if line.startswith("INSERT"):
                    try:
                        cursor.execute(line)
                    except Exception, strerror:
                        print "Error on loading fixture:"
                        print "-- ", strerror
                        print "-- ", line
    
            print "Fixtures loaded"
    
        except AttributeError:
            print "FIXTURES_FILE not found in settings. Please set the FIXTURES_FILE in \
                your settings.py" 
    
        cursor.close()
        db.commit()
        db.close()
    
    signals.post_syncdb.connect(load_fixtures, sender=auth_app, \
        dispatch_uid = "<myproject>.<myapp>.management.load_fixtures")
    

    And in my settings.py I added FIXTURES_FILE with the path to my .sql file with the sql dump.

    One thing that I still haven't found is how to fire this signal only after the tables are created, and not everytime syncdb is fired. A temporary work around for this is use INSERT IGNORE INTO in my sql command.

    I know this solution is far from perfect, and critics/improvements/opinions are very welcome!

    Regards,

    Aldo