0

This question is similar to this: field choices() as queryset?

For example if I have a really simple model:

class Order(models.Model):
    quantity = models.FloatField()        
    def __unicode__(self):
        return self.quantity

And the modelform is like so:

class OrderForm(models.Modelform):
    class Meta:
        model = Order

Then I have some queryset from another model, ie. I pull the names of all the items:

items = [item.name for item in Inventory.objects.all()]

How do I generate a quantity field for each item in this list and let the verbose name of each of those fields be the name of the each item? will I need some sort of formset?

Community
  • 1
  • 1
atma
  • 1
  • 1

1 Answers1

0
class OrderForm(models.Modelform):
    class Meta:
        model = Order

    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)

        self.fields['quantity'].choices = [(i.quantity, i.name) for i in Inventory.objects.all()]
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444