Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question