I’d like to do something like this in Django:
class MyForm(forms.Form):
items = Items.objects.all()
for item in items:
# How does this part work?
exec(item.name) = forms.BooleanField()
The goal is to create one form field for each item returned from the database query. So, if I get ten items back from the query, then the class would have ten variables in it, each named after a returned item.
This seems theoretically possible, but is there some danger here? The items in the database are not user generated.
You can dynamically modify a form however you please:
I suppose the danger is if the database state changes while a user is submitting a form, and the new form initializes with new fields which the previous form didn’t have.