I want to make a form that’s a list of all objects in a model with number inputs after them by using a for loop instead of typing out each field manually. How could this be accomplished? Thanks in advance to anyone who wants to respond.
What I’m trying to do:
class OrderForm(forms.Form):
for p in [Model].objects.all():
field = forms.IntegerField(label=p.product_text)
As far as I’m aware you really want to use Formset and not to add various number of the same fields. However if not, then you can create a new class dynamically
OrderForm = type(
'Form',
(forms.Form,),
{
'p%s' % key: forms.IntegerField(label=p.product_text)
for key, field in enumerate(Model.objects.all())
},
)