I
I
Irina2021-09-24 05:09:13
Django
Irina, 2021-09-24 05:09:13

How to generate form fields in django?

I'm a complete newbie to django and don't really understand how things work here. There was a task - to create a form with many different fields (1000+) from a table with a description of these fields. It seemed that prescribing them all manually is irrational, and there is definitely a simpler and more elegant way.
In a separate script, I pulled out all the necessary data from csv ( test_fields - a list of dictionaries with the necessary fields and their parameters), imported it, tried to generate the necessary fields in models.py using a loop and some perversions:

test_names = []


class Test(models.Model):
    for field in test_fields:
        name = field['name']
        if field['type'] == 'text':
            globals()[name] = models.TextField(name, default=field['description'])
            test_names.append(name)

With the help of test_names , I planned to manipulate the fields in the future (these names must be stored in the database itself). In parts, everything works adequately, but the fields themselves are not created and the corresponding error is raised during migration.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question