简体   繁体   中英

Assign each value using different loops in python

Hi I got a loop where I get different parameters (age, date, role, etc.), and I got an array where I get these values. How can I assign each value to eache parameter. I'll add some code below

I tried this but obviosly didn't work

parameters = Parametros.objects.filter(job_id=job.id)
        if request.method == 'POST':    
            for parameter in parameters:
                #Update parameters
                params = request.POST.getlist('parameter')
                for i in range(len(params)):
                    cursor.execute("UPDATE Jobs_parametros SET parameter_value='" + params[i] + "' WHERE parameter_name='" + parameter.parameter_name + "' AND job_id=" + str(job.id))

Here we can see "parameters" where I'm assigning parameter.parameter.name for the different names. After that we can see the array where I get the values called "params". I want to assign each value I get from params to the name of the parameter to execute the code showed in the last line

The result that I got with the code showed is each parameter name with the last value I added. The values are updated but only take the last value in my form and didn't assigned my values like the date and age for eache parameter name

在此处输入图像描述

It seems nobody could resolve my question, instead answered woth useless answers so here is how i could resolve it

parameters = Parametros.objects.filter(job_id=job.id)
        aux = 0
        if request.method == 'POST':    
            for parameter in parameters:
                #update parameters
                aux = aux + 1
                values = request.POST.getlist('parameter')
                for i in range(len(values)):
                    if (i + 1) == aux:
                        cursor.execute("UPDATE Jobs_parametros SET parameter_value='" + values[i] + "' WHERE parameter_name='" + parameter.parameter_name + "' AND job_id='" + str(job.id) + "'")

I used an aux to match index numbers for each array. It was simple logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM