Django Shell 文件.py 导入功能

2024-04-24 21:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我是通过哨兵的命令来尝试的:

sentry --config=sentry.conf.py shell < add_user.py

添加_用户.py看起来像这样:

from django.contrib.auth.models import User

try:
    import authfile
except Exception as e:
    print "authfile not present, %s" % e

def conv_dict(the_dict):
    sen_username = the_dict['sen_username']
    sen_email = the_dict['sen_email']
    sen_password = the_dict['sen_password']
    print sen_username
    return sen_username, sen_email, sen_password

for t, imp_dict in authfile.__dict__.iteritems():
    if isinstance(imp_dict, dict) and not t.startswith('_'):
        sen_username, sen_email, sen_password = conv_dict(imp_dict)
        try:
            new_user = User.objects.create_user( sen_username, sen_email, sen_password)
            new_user.is_superuser = True
            print "created new user: %s" % imp_dict['sen_username']
        except Exception as e:
            print "could not create new user: %s" % e
            pass

authfile是一组字典,用于填充for循环中调用的值。你知道吗

如果我运行交互式shell并导入这个文件,它就会工作。如果我尝试使用<;将其拉入,它将不运行任何输出和出口,但它不会像在shell中以交互方式运行时那样创建用户。输出如下所示:

sentry --config=../sentry.conf.py shell < add_super.py
Python 2.7.3 (default, Aug  9 2012, 17:23:57)
Type "copyright", "credits" or "license" for more information.

IPython 1.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
In [2]:
In [2]:
In [3]:
In [3]:    ...:    ...:    ...:    ...:    ...:    ...:
In [4]:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:
Do you really want to exit ([y]/n)?

Tags: theinpyforemailusernamepasswordshell