A
A
Alex P.2018-07-22 01:58:40
Python
Alex P., 2018-07-22 01:58:40

How to generate a form on the go?

Idea such:
To take from a DB headings of columns and on the move for different users to generate forms.
For example:
There is a class that pulls out the headings of the columns of a table in the form of a list

class DbFields:  # returns col-names from base for rendering forms
    @staticmethod
    def get_col_names(name):  # name - table name
        a = 'select * from ' + name
        cursor.execute(a)
        return [member[0] for member in cursor.description]

at the output we get something like ['id', 'item', 'about', 'size', 'weit']
and now for User1, for example, form a form with the fields 'item', 'size', 'weit '
and here I have a stupor ..
scribbled a class, but the output is a list of field objects
class UserInput(FlaskForm):
    @staticmethod
    def form_render(table):
        fields = []
        names = DbFields.get_col_names(table)
        for i in range(len(names)):
            f = StringField(names[i], validators=[DataRequired()])
            fields.append(f)
            return fields

tell me where to dig further?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2018-07-22
@Big_Alex

class MyForm(FlaskForm):
    @classmethod
    def dynamicForm(cls):
        for n in ['field1', 'field2', 'field3']:
            setattr(cls, n, StringField(n))
        return cls()


form = MyForm.dynamicForm()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question