如何在Django中为用户模型加载sql fixture? [英] How to load sql fixture in Django for User model?

查看:137
本文介绍了如何在Django中为用户模型加载sql fixture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何使用sql灯具加载auth.User的初始数据?
对于我的模型,我刚刚有一个< modelname> .sql文件在一个名为sql的文件夹中,syncdb的工作精美。但我不知道如何为auth.User模型做。我已经google了,但没有成功。



提前感谢



Aldo

解决方案

感谢您的答案。我找到了适用于我的解决方案,巧合是布赖恩的建议之一。这是:



冷杉我断开了syncdb后创建超级用户的信号,因为我的超级用户在我的auth_user灯具中:



models.py

  from django.db.models import从django.contrib.auth.management将
的信号从django.contrib.auth导入create_superuser
作为auth_app


signals.post_syncdb.disconnect(
create_superuser
sender = auth_app,
dispatch_uid =django.contrib.auth.management.create_superuser)

然后我创建了一个在syncdb后调用的信号:



myproject> /< myapp> / management / __ init __。py

 
加载sql /来自django.db.models的
导入get_models,来自django.conf的
导入设置
import< myproject>。< myapp>< modelname> .sql
; .models as auth_app

def load_fixtures(app,** kwargs):
import MySQLdb
db = MySQLdb.connect(host = settings.DATABASE_HOST或localhost,\\ \\
user = settings.DATABASE_USER,
passwd = settings.DATABASE_PASSWORD,port = int(settings.DATABASE_PORT或3306))

cursor = db.cursor()

尝试:
打印从文件%s加载灯具到%s。 %(settings.DATABASE_NAME,\
settings.FIXTURES_FILE)
f = open(settings.FIXTURES_FILE,'r')
cursor.execute(use%s;%settings.DATABASE_NAME)
f中的行
如果line.startswith(INSERT):
try:
cursor.execute(line)
除了异常,strerror:
打印加载灯具错误:
打印 - ,strerror
打印 - ,行

打印装载

除了AttributeError:
打印设置中找不到FIXTURES_FILE请在\
中设置FIXTURES_FILE您的settings.py

cursor.close()
db.commit()
db.close()

signals.post_syncdb.connect(load_fixtures,sender = auth_app,\
dispatch_uid =< myproject>。< ; myapp> .management.load_fixtures)

而在我的 settings.py 中,我添加了FIXTURES_FILE,带有sql转储文件的路径。



我仍然没有找到的一件事是仅在创建表之后才能触发此信号,而不是每次syncdb触发。我的sql命令中的临时工作是使用INSERT IGNORE INTO。



我知道这个解决方案远非完美,评论/改进/意见非常受欢迎!



问候,



Aldo


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

解决方案

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

这篇关于如何在Django中为用户模型加载sql fixture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆