Search code examples
pythondjangopostgresqlcursorpsycopg2

Django Cursor.Execute Issue with Postgresql


I ran into a problem with cursor.execute, moved onto a completely separate part of my program, and ran into the same issue. I get that the value of a cursor return is: "django.db.backends.util.CursorDebugWrapper object". My code in my view is:

model_id.execute("SELECT id FROM \"{0}\".\"{1}\" where model_region='{2}'".format("public","pecasRunLog_modelinstance", model_name))

In another .py file I have:

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))

These calls are used in the following code snippet:

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)

The purpose of this code is to get log-in data, then post model run data and comments from a python script. The script works correctly without comments (r3 above). I need the "region_id" and "run_id" because they are foreign keys between Django models.


Solution

  • I fixed my own error. The cursor was only being applied to the first call, then being consumed. I switched it around to create a variable with the value output by the cursor call. It was an issue of lack of understanding of the functionality of the cursor function.