How to dynamically change variable name in form.vars.var_name

2024/10/15 11:22:01

I have defined counter variable in controller.

I can define tables and fields dynamically.

tables = [db.define_table('example_table_%s' % x,Field('example_field_%s' % x, type='string', ...)...)for x in range(0, counter+1)]

I can add defined tables to SQLFORM.

form = SQLFORM.factory(db.table_1,db.table_2,*tables,submit_button='Submit')

I can control process manually if the counter value is '2'.

if form.process.accepted():id = db.table_1.insert(**db.table_1._filter_fields(form.vars))form.vars.table_1_field = idid = db.table_2.insert(**db.table_2._filter_fields(form.vars))#'0'form.vars.table_2_field = form.vars.example_field_0id = db.table_2.insert(**db.table_2._filter_fields(form.vars))#'1'form.vars.table_2_field = form.vars.example_field_1id = db.table_2.insert(**db.table_2._filter_fields(form.vars))#'2'form.vars.table_2_field = form.vars.example_field_2id = db.table_2.insert(**db.table_2._filter_fields(form.vars))

Above code works on my application.

How do I dynamically do the #'0', #'1' and #'2' steps?

Answer
for i in range(0, counter + 1):form.vars.table_2_field = form.vars['example_field_%s' % i]db.table_2.insert(**db.table_2._filter_fields(form.vars))

Also, if you are not using the dynamically generated tables to store data in the database, be sure to set migrate=False in the table definitions -- otherwise, the DAL will actually create each of those tables in the database.

https://en.xdnf.cn/q/117840.html

Related Q&A

Why will one loop modify a list of lists, but the other wont [duplicate]

This question already has answers here:Python list doesnt reflect variable change(6 answers)Closed 8 years ago.One of the answers in "python way" to parse and conditionally replace every elem…

Python CGI executable script downloads / shows script code

A script I wrote long ago (getWords.py) used to be executable at my localhost (http://local.example.com/getWords.py?query-string)My python script starts like this :#!/usr/bin/env python # chmod 755 ge…

My function returns None

I am new to Python and I was trying to solve this exercise, but keep getting None output. The question asked for a program in which the input is hours and rate and the output is the gross pay, includin…

Django undefined symbol: PyUnicode_AsUTF8

I am new to Python/Django. I have set up the environment needed to run Django project.When Im trying to migrate an existing project , it shows up this errordjango.core.exceptions.ImproperlyConfigured: …

using a variable keyword for an optional argument name with python argparse

I am using argparse for a python script I am writing. The purpose of the script is to process a large ascii file storing tabular data. The script just provides a convenient front-end for a class I have…

Error while setting up MongoDB with django using django mongodb engine on windows

Steps I followed :pip install git+htp://github.com/django-nonrel/[email protected]It did not work, so I downloaded the zip from the site "htp://github.com/django-nonrel/django" and pasted the…

Python login page with pop up windows

I want to access webpages and print the source codes with python, most of them require login at first place. I have similar problem before and I have solved it with the following code, because they are…

Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST

I am reproducing a chart in a paper using the LOGNORM.DIST in Microsoft Excel 2013 and would like to get the same chart in Python. I am getting the correct answer in excel, but not in python.In excel …

Python MySQLdb cursor.execute() insert with varying number of values

Similar questions have been asked, but all of them - for example This One deals only with specified number of values.for example, I tried to do this the following way:def insert_values(table, columns, …

Searching in a .txt file and Comparing the two values of a string in python?

"cadence_regulatable_result": "completeRecognition","appserver_results": {"status": "success","final_response": 0,"payload": {"…