title: Django form with inputs populated from database
body: <p>I have a simple Django model structure with Products and Orders, linked by an intermediary table that determines the quantity of each product within a given order:</p>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class Product(models.Model):
      name = models.CharField()
  
  class Order(models.Model):
      id = models.IntegerField()
      products = models.ManyToManyField(Product, through='OrderProduct')
  
  class OrderProduct(models.Model):
      order=models.ForeignKey(Order, on_delete=models.CASCADE)
      product = models.ForeignKey(Product, on_delete=models.CASCADE)
      quantity = models.PositiveSmallIntegerField()
  </code></pre>
  
  <p>I'd like to design a form in my forms.py file that 1) pulls a list of every product in the database; 2) gives the user the option to adjust the quantity of each product ordered (e.g., in a CharField input), and 3) creates new Order and OrderProduct objects that store a record of the quantity of each Product ordered by the user.</p>
  
  <p>However, I can't figure out a good way to create a form that will automatically pull all Products from the database and add fields to represent the quantity ordered of the given Product. Ideally the result would look something like this, but coded automatically:</p>
  
  <p><strong>forms.py:</strong></p>
  
  <pre><code>class OrderForm(forms.Form):
      product_1_quantity = forms.IntegerField()
      product_2_quantity = forms.IntegerField()
      product_3_quantity = forms.IntegerField()
  </code></pre>
  
  <p>....etc., for each product. What's the best way to do this? Does it involve a 'for' loop that creates a new field for each product found in the database?</p>