Home > Software engineering >  Assign each value using different loops in python
Assign each value using different loops in python

Time:01-27

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

enter image description here

CodePudding user response:

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

  • Related