I have to run a query with exec in my python code:
strfin = ''
a = []
a = qsearch.split(',')
for li in range(0, len(a)):
b = a[li].split(':')
if b[0].upper() == 'ID':
strfin = strfin + ',id__contains=' + "'"+b[1]+"'"
elif b[0].upper() == 'TEST NAME':
strfin = strfin + ',id_test__test_main__descr__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'TEST TYPE':
strfin = strfin + ',thread_ttype__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'TEST GROUP':
strfin = strfin + ',thread_tgroup__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'SCHEDULE':
strfin = strfin + ',thread_stype__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'THREAD NAME':
strfin = strfin + ',thread_main__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'START':
strfin = strfin + ',thread_startd__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'STOP':
strfin = strfin + ',thread_stopd__contains=' + "'" + b[1].strip() + "'"
elif b[0].upper() == 'USER':
strfin = strfin + ',id_test__user_id__contains=' + "'" + b[1].strip() + "'"
afilexec = "%s%s%s" % ("activeThreads = t_threads.objects.filter(~Q(thread_status='DEAD'),", strfin[1:], ").select_related().distinct('thread_stag')")
if i at the end of code write:
exec(afilexec)
System return error:
SyntaxError: unqualified exec is not allowed in function 'tabrefresh' because it contains a nested function with free variables
i try to trasform this exec from unqualified to qualified like this:
exec afilexec in {}
no error in comiling but when i run my function i got:
exec afilexec in {} File "", line 1, in NameError: name 't_threads' is not defined
How can i exec this query in my code?
Thanks in adcance