【发布时间】:2014-08-26 05:23:59
【问题描述】:
我遇到了 cursor.execute 的问题,移到了我程序的一个完全独立的部分,并遇到了同样的问题。我知道游标返回的值是:“django.db.backends.util.CursorDebugWrapper 对象”。我认为我的代码是:
model_id.execute("SELECT id FROM \"{0}\".\"{1}\" where model_region='{2}'".format("public","pecasRunLog_modelinstance", model_name))
在另一个 .py 文件中:
region_id=connections['default'].cursor()
region_id.execute("SELECT id FROM \"pecasRunLog_modelinstance\" where model_region='{0}'".format(region))
run_id=connections['default'].cursor()
run_id.execute("SELECT id FROM \"pecasRunLog_runinstance\" where run_name='{0}'".format(schema+timestamp))
这些调用用在以下代码sn-p中:
url_login='/pecasRunLog/accounts/login/'
url_add_run='/pecasRunLog/model/'+region+'/add_run/'
url_add_comment='/pecasRunLog/model/'+region+'/'+schema+timestamp+'/add_comment/'
client = requests.session()
client.get(url_login)
csrftoken = client.cookies['csrftoken']
login_data = {'username':user,'password':password, 'csrfmiddlewaretoken':csrftoken, 'next': '/pecasRunLog/'}
r1=client.post(url_login,data=login_data)
run_data={'model_region':region_id,'scendir':scendir, 'mapit_scenario': schema, 'run_name':schema+timestamp, 'run_computer_name':os.environ['COMPUTERNAME'], 'run_computer_ip':get_lan_ip(), 'declared_user':declared_user, 'logged_in_user':getpass.getuser(), 'sd_schema':schema, 'sd_database':database, 'sd_host':get_lan_ip(), 'sd_port':pgport,'mapit_schema':schema, 'mapit_database':database, 'mapit_host':get_lan_ip(), 'mapit_port':pgport,'start_date':start_date, 'start_time':start_time, 'end_date':end_date, 'end_time':end_time,'logged_manually':3}
r2=client.post(url_add_run,data=run_data)
comment_data={'model_region':region_id, 'run_name':run_id, 'comment':run_comment}
r3=client.post(url_add_comment,data=comment_data)
此代码的目的是获取登录数据,然后从 python 脚本发布模型运行数据和 cmets。该脚本在没有 cmets(上面的 r3)的情况下正常工作。我需要“region_id”和“run_id”,因为它们是 Django 模型之间的外键。
【问题讨论】:
-
如何声明游标(model_id、run_id、region_id)?
-
我用附加代码更新了问题的第二部分。第一部分(用于 model_id)类似,因为它有两个连续的游标调用。
标签: python django postgresql cursor psycopg2